Unverified Commit f00cb51e authored by Jake Goodman's avatar Jake Goodman Committed by GitHub
Browse files

Merge pull request #1 from jakegoodman01/setup-square

Setup square
parents 57398656 59039b44
#ifndef _PIECE_H_
#define _PIECE_H_
enum Color { WHITE, BLACK, BOTH };
enum Piece {
EMPTY,
wP, wN, wB, wR, wQ, wK,
bP, bN, bB, bR, bQ, bK,
OFFBOARD
};
const int PieceVal[] = {
0,
100, 325, 325, 550, 1000, 9999,
100, 325, 325, 550, 1000, 9999,
0
};
const int PieceColor[] = {
BOTH,
WHITE, WHITE, WHITE, WHITE, WHITE, WHITE,
BLACK, BLACK, BLACK, BLACK, BLACK, BLACK,
BOTH,
};
inline bool isValidPiece(int p) {
// Produces true if the piece is not EMPTY or OFFBOARD
return !(p == EMPTY || p == OFFBOARD);
}
#endif
#include <cassert>
#include <iomanip>
#include "position.h"
Position::Position() {
for (int i = 0; i < BOARD_SQ_NUM; ++i) {
squareList[i] = (isValidSquare(i)) ? EMPTY : OFFBOARD;
}
}
void Position::placePiece(Piece p, Square s) {
assert(isValidPiece(p));
assert(isValidSquare(s));
squareList[s] = p;
}
Piece Position::removePiece(Square s) {
assert(isValidSquare(s));
assert(isValidPiece(squareList[s]));
Piece p = squareList[s];
squareList[s] = EMPTY;
return p;
}
std::ostream &operator<<(std::ostream &out, const Position &p) {
for (int i = 0; i < BOARD_SQ_NUM; ++i) {
if (isValidSquare(i)) {
out << std::setw(4) << p.squareList[i];
if (squareToFile(i) == FILE_H) out << std::endl;
}
}
return out;
}
\ No newline at end of file
#ifndef _POSITION_H_
#define _POSITION_H_
#include <iostream>
#include "piece.h"
#include "square.h"
extern const int BOARD_SQ_NUM;
class Position {
private:
Piece squareList[BOARD_SQ_NUM];
public:
// Default constructor initializes the squareList
Position();
// placePiece puts the given piece on the given square
// Requires:
// * piece is not EMPTY or OFFBOARD
// * square is a valid square on the chess board
void placePiece(Piece, Square);
// removePiece removes and returns the piece on the given square
// Requires:
// * square is a valid square on the chess board
// * the piece occupying the square is not EMPTY of OFFBOARD
Piece removePiece(Square);
// Prints the pieces on each valid square
friend std::ostream &operator<<(std::ostream &, const Position &);
};
#endif
#ifndef _SQUARE_H_
#define _SQUARE_H_
#include <cassert>
const int BOARD_SQ_NUM = 120;
enum Square {
A1 = 21, B1, C1, D1, E1, F1, G1, H1,
A2 = 31, B2, C2, D2, E2, F2, G2, H2,
A3 = 41, B3, C3, D3, E3, F3, G3, H3,
A4 = 51, B4, C4, D4, E4, F4, G4, H4,
A5 = 61, B5, C5, D5, E5, F5, G5, H5,
A6 = 71, B6, C6, D6, E6, F6, G6, H6,
A7 = 81, B7, C7, D7, E7, F7, G7, H7,
A8 = 91, B8, C8, D8, E8, F8, G8, H8, NO_SQ
};
enum File { FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NONE };
enum Rank { RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NONE };
inline int squareToFile(int square) {
if (A1 <= square && square <= H8) {
switch (square % 10) {
case 1: return FILE_A;
case 2: return FILE_B;
case 3: return FILE_C;
case 4: return FILE_D;
case 5: return FILE_E;
case 6: return FILE_F;
case 7: return FILE_G;
case 8: return FILE_H;
}
}
return FILE_NONE;
}
inline int squareToRank(int square) {
if ( !(square % 10 == 0 || square % 10 == 9) ) {
switch (square / 10) {
case 2: return RANK_1;
case 3: return RANK_2;
case 4: return RANK_3;
case 5: return RANK_4;
case 6: return RANK_5;
case 7: return RANK_6;
case 8: return RANK_7;
case 9: return RANK_8;
}
}
return RANK_NONE;
}
inline bool isValidSquare(int sq120) {
// Produces true if the square is not on FILE_NONE and not on RANK_NONE
return !(squareToFile(sq120) == FILE_NONE || squareToRank(sq120) == RANK_NONE);
}
inline int square120to64(int sq120) {
assert(isValidSquare(sq120));
return 8 * squareToRank(sq120) + squareToFile(sq120);
}
inline int square64to120(int sq64) {
assert(0 <= sq64 && sq64 <= 63);
if (sq64 < 8) return A1 + sq64;
if (sq64 < 16) return A2 + sq64 % 8;
if (sq64 < 24) return A3 + sq64 % 8;
if (sq64 < 32) return A4 + sq64 % 8;
if (sq64 < 40) return A5 + sq64 % 8;
if (sq64 < 48) return A6 + sq64 % 8;
if (sq64 < 56) return A7 + sq64 % 8;
return A8 + sq64 % 8;
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment