#include #include "Grid.h" // Constructor for Grid // Initialise the grid // to void and creates // player objects and umpire. // Also starts playing. Grid::Grid() { int i,j; for (i= 0; i < 3; i++) { for (j = 0; j < 3; j++) { grid[i][j] = ' '; } } xPlayer = new Player(this,'X'); oPlayer = new Player(this,'0'); umpire = new Umpire(); curToken = 'X'; xPlayer->makeAMove(); } // We need to delete the objects // we have created to // avoid memory leaks Grid::~Grid() { delete xPlayer; delete oPlayer; delete umpire; } // Prints the grid in a visible format void Grid::printGrid() { int i,j; cout << " "; for (i = 0; i < 3; i++) { cout << i <<" "; } cout << endl; for (i= 0; i < 3; i++) { cout << i << " "; for (j = 0; j < 3; j++) { cout << grid[i][j] << " "; } cout << endl; } cout << endl; } // Key function of the program // Checks if tbe move is valid // And ask players to play in turn void Grid::move(int r, int c) { char returned; cout << "Move is " << curToken << " " << r << " " << c << endl; returned = umpire->validate(curToken,r,c); switch(returned) { case 'v': grid[r][c] = curToken; if (curToken == 'O') curToken = 'X'; else curToken = 'O'; if (curToken=='X') xPlayer->makeAMove(); else oPlayer->makeAMove(); break; case 'i': cout << "Invalid move - try again" << endl; if (curToken=='X') xPlayer->makeAMove(); else oPlayer->makeAMove(); break; case 'w': cout<< curToken << "Player wins" << endl; break; case 'd': grid[r][c] = curToken; cout << "It's a draw." << endl; printGrid(); break; default: cout << "Odd return " << endl; } }