#include using namespace std; #include "board.h" const int SIZE = 16; int Board::size() const { return SIZE; } Board::Board() : mySquares(SIZE, empty), myCount(0) { int k; for(k=0; k < SIZE; k++) { myString += " "; } } string Board::tostring() const { return myString; } bool Board::isFull() const { return myCount >= SIZE; } bool Board::isClear(int index) const { if (0 <= index && index < SIZE) { return mySquares[index] == Board::empty; } return false; } bool Board::isWin(Board::Player p) const { if (p == Board::empty) return false; int k; for(k=0; k <= 12; k+= 4) { if (horizWin(k, p)) { return true; } } for(k=0; k <= 3; k++) { if (vertWin(k,p)) { return true; } } if (mySquares[0] == mySquares[5] && mySquares[5] == mySquares[10] && mySquares[10] == mySquares[15] && mySquares[0] == p) { return true; } if (mySquares[3] == mySquares[6] && mySquares[6] == mySquares[9] && mySquares[9] == mySquares[12] && mySquares[3] == p) { return true; } return false; } bool Board::horizWin(int index, Board::Player p) const { if (mySquares[index] == mySquares[index+1] && mySquares[index+1] == mySquares[index+2] && mySquares[index+2] == mySquares[index+3] && mySquares[index] == p) { return true; } return false; } bool Board::vertWin(int index, Board::Player p) const { if (mySquares[index] == mySquares[index+4] && mySquares[index+4] == mySquares[index+8] && mySquares[index+8] == mySquares[index+12] && mySquares[index] == p) { return true; } return false; } void Board::unplace(int index) { mySquares[index] = Board::empty; myString[index] = ' '; myCount--; } void Board::place(int index, Board::Player p) { static char labels[] = {'X', '0'}; if (isClear(index)) { mySquares[index] = p; myString[index] = labels[p]; myCount++; } } int Board::clearCount() const { return SIZE - myCount; } void Board::print() const { static string playerStrings[] = {" X ", " O ", " "}; int j; for(j=0; j < SIZE; j++) { if (j % 4 == 0) { cout << endl; } if (mySquares[j] == Board::empty) { cout << " " << j << " "; } else { cout << playerStrings[mySquares[j]]; } } cout << endl; }