// // Support code for Tic-Tac-Toe program // // The game board is a 3x3 array of ints // board[3][3]: // board[0][0] is upper left corner // board[0][1] is top row, middle // ... // board[2][2] is bottom right // 0 means the spot is empty // 1 means 'X' has the spot // 2 means 'O' has the spot #include // needed for cout and cin // drawpos will draw the right character to the screen, depending // on the value of the parameter val. // if val is 0, the spot is empty to draw a blank // if val is 1, the spot belongs to player X, print 'X' // if val is 2, the spot belongs to player O, print 'O' void drawpos( int val ) { if (val==0) cout << " "; // val is 0, so draw a space else if (val==1) cout << "X"; // val is 1, so draw a 'X' else if (val==2) cout << "O"; // val is 2, so draw a 'O' } // drawrow gets an array of 3 ints and draw a // row of a tic-tac-toe game using the array to determine // what should go in each of the three squares. // // draws a row of the board void drawrow( int row[3] ) { cout << " "; // indent a space drawpos(row[0]); // draw the leftmost spot cout << " | "; // draw the vertial bar drawpos(row[1]); // draw the middle cout << " | "; // draw the vertial bar drawpos(row[2]); // draw the rightmost spot cout << endl; // newline (end of the line) } // drawgame will print out a tic-tac-toe game board // // the output looks something like this: // // | | // ---|---|--- // X | | O // ---|---|--- // X | O | // // // the top left corner is board[0][0] // the top middle is board[0][1] // the bottom right corner is board[2][2] void drawgame( int board[3][3] ) { drawrow(board[0]); // draws the top row cout << "---|---|---" << endl; drawrow(board[1]); // draws the middlerow cout << "---|---|---" << endl; drawrow(board[2]); // draws the bottom row } // Sample main just creates a board that represents a game // already in progress (has some 1s and 2s in it) and // draws the board. // // You should replace this with your own main() function that // plays a game. int main(void) { int b[3][3] = { {0,0,0}, {1,0,2}, {1,2,0}}; drawgame(b); } // Some Suggestions on how to complete this assignment // (just suggestions - do it any way you like!). // // write a function that changes a tic-tac-toe board // (a 3x3 array of ints) by replacing a 0 with a 1. // This function is the "brain" of the game. It should do // at least the following: // 1. always win if it can // 2. always block a loss if it can. // // it doesn't need to be any smarter than the 2 requirements above, // but it might be fun (if you have time) to make it check for other // conditions before moving. // In order to write the above function, you probably want some functions // that do things like count the number of 1s in a row or column or diagonal // or count the number of 2s in a row, column or diagonal. Here is an idea to // get you started: // // write the function show below, which returns true if // the player can win the game given the board. // The board is passed in and the player (which can be a 1 or a 2) // // int canwin_board(int board[3][3], player); // // to write this function you probably want something that can test // a row, column or diagonal - here is an idea: // // write canwin_seq that checks a set of 3 board values to see // if the player can win. The idea is tl call this for each row, // column and diagnonal. // // int canwin_seq( int x1, int x2, int x3, int player); // this function probably does something like see if // 2 of the 3 values (the xs) are equal to player and the other is 0. // // now canwin_board can do something like call canwin_seq for each // row and column, here are a few of them: // // canwin_seq(board[0][0], board[0][1], board[0][2], player) // top row // ... // canwin_seq(board[0][0], board[1][1], board[2][2], player) // a diagonal // ... // canwin_seq(board[0][2], board[1][2], board[2][2], player) // right column // // // Need more help getting started? Send email to cpp@cs.rpi.edu