Problem 1 Tiotactoe Write the function implementatios for a

Problem 1: (Tio-tactoe) Write the function implementatios for a progam that plays the Tk tac-toe gae on a 4x&board.; Player goes first, and player O gesd The struct Struct ticTacTeeBoard nt cuEE-player int points[4 4) acodes the state of the game curr-player3 ms it\'s\'s turm to play. curr player- mrans it\'s 0\'s turn to play. poiste(4 41 contains the movesmade so lar. An empty point has value Q. a pont marked X has vale 1. and a point marked O has value -1 The procedure void pristBoard(escTacToeBoard board) peints the Ihoard. The delfinition of priatBoard is peovided, and it wil clarily the meaning of ticTacToeBoard. The procedure vold isitBoard CscTacToeBoard& beard) initialiaes the board Since X goes first, initBoard t set board.curr player to 1 Since the board is empty at the beginning of the game, isitBeard set all valoes o the aay board points to The predicate bool iskapty(sicTacToeleard board, ist xy) checks if the poin (x,y) is empty and therefee availabile to play. The procedure vold sark(ticTacToeBoardi board, ist x, it ) marks the point Cx.y). For example, i beard curr player is -1 (so in\'s O\'s turn to play) and we wish to maak (1,1) then board poistso4-0) st be assigned to -1 The peedicale bool beardFwl1(ticTacToeBeard board) returns true if the board is full and false othewise The unction ist winser(eacToeBoard board)i no wintrr, 1 i1 X i-the winner·and-1if0is\' thr winner returns 0 if thrre The function ist ,\" is ( ); shows how we lstend to use these functions. In particular it shows that the points are refered to with 1-based indexing. So Xmarkd (2,1) and marked (3,4). the board will print to I1 101 Hist In writing visser you may ind a helper Janction useful Helper functions are functons intesded to aild ot fuctions but not Istended to be used by itselt. Foe example, you could write a functice o1 visnerHelper (tscTacTeeBoard board, ist player) that Peturns true if player is a winter abd false otherwbe Theti wianer will merely call vinserHelper twoe.

Solution

#include <iostream>
using namespace std;

struct ticTacToeBoard{
   int curr_player; //1 or -1
   int points[4*4]; // 0 for \'\', 1 for \'X\', -1 for \'O\'
};

void printBoard( ticTacToeBoard board ){
   for(int j = 0; j < 4; j++){
       for(int i = 0; i < 4; i++){
           cout << \"|\";
           if( board.points[ i*4 +j ] == 0 ){ cout << \" \"; }
           else if( board.points[ i*4 + j ] == 1 ){ cout << \"X\"; }
           else{ cout << \"O\"; }
       }
       cout << \"|\" << endl;
   }
}

void initBoard( ticTacToeBoard& board ){
   board.curr_player = 1;
   for(int i = 0; i < 4; i++){
       for(int j =0; j < 4; j++){
           board.points[ i*4 + j ] = 0;
       }
   }
}

bool isEmpty( ticTacToeBoard board, int x, int y ){ return board.points[ (x-1)*4 + (y-1) ] == 0; }

void mark( ticTacToeBoard& board, int x, int y){
   if( isEmpty(board,x,y) ){
       board.points[ (x-1)*4 + (y-1) ] = board.curr_player;
   }
}

bool boardFull( ticTacToeBoard board ){
   for(int i = 0; i < 16; i++){
       if( board.points[i] == 0 ){ return false; }
   }
   return true;
}

bool winnerHelper( ticTacToeBoard board, int player ){
   for(int i = 0; i < 4; i++ ){
       if( (board.points[i*4 + 0] == player && board.points[i*4 + 1] == player) &&
           (board.points[i*4 + 2] == player && board.points[i*4 + 3] == player ) ){
           return true;
       }
   }
   for(int i = 0; i < 4; i++ ){
       if( (board.points[0*4 + i] == player && board.points[1*4 + i] == player) &&
           (board.points[2*4 + i] == player && board.points[3*4 + i] == player ) ){
           return true;
       }
   }
   if( (board.points[ 0*4 + 0 ] == player && board.points[ 1*4 + 1] == player) &&
           (board.points[2*4 + 2] == player && board.points[ 3*4 + 3] == player ) ){
           return true; }
   if( (board.points[ 0*4 + 3 ] == player && board.points[ 1*4 + 2] == player) &&
           (board.points[2*4 + 1] == player && board.points[ 3*4 + 0] == player ) ){
           return true; }
   return false;
}

int winner( ticTacToeBoard board ){
   if( winnerHelper( board, 1 ) ){ return 1; }
   if( winnerHelper( board, -1 )){ return -1; }
   return 0;
}

int main(){
   ticTacToeBoard board;
   initBoard( board );
   int x,y;
   while( winner(board) == 0 && !boardFull(board) ){
       printBoard( board );
       cout << \"Player \" << board.curr_player << \" \" << \": Enter your move x y \" << endl;
       cin >> x >> y;
       mark( board, x , y );
       board.curr_player *= -1;
   }
   printBoard( board );
   int win = winner( board );
   if( win == 0 ){ cout << \"DRAW!!!\" << endl; }
   cout << \"Player \" << win << \" wins!!!\" << endl;
}

 Problem 1: (Tio-tactoe) Write the function implementatios for a progam that plays the Tk tac-toe gae on a 4x&board.; Player goes first, and player O gesd T
 Problem 1: (Tio-tactoe) Write the function implementatios for a progam that plays the Tk tac-toe gae on a 4x&board.; Player goes first, and player O gesd T

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site