/* CSCI261 Homework 09: Tic Tac Toe * * Author: YOUR NAME * * Description: DESCRIBE HERE WHAT THIS PROGRAM DOES * */ /***** PREPROCESSOR DIRECTIVES *****/ #include #include #include #include using namespace std; #include using namespace sf; /***** FUNCTION PROTOTPYES *****/ /***** SEE BELOW MAIN FOR FUNCTION DETAILS *****/ bool IsMoveValid( char board[3][3], int row, int col ); int IsGameOver( char board[3][3] ); void ComputerMove( char board[3][3] ); void DrawTicTacToeBoard( RenderWindow &win ); void DrawO( RenderWindow &win, int row, int col ); void DrawX( RenderWindow &win, int row, int col ); /***** OUR MAIN FUNCTION! *****/ int main() { /****** BEGIN PART I - CREATE THE GAME BOARD ******/ /****** END PART I ******/ srand( time(NULL) ); // seed our random number generator int gameOver = 0; // initial game state is not over // creates the window object with a 640x640 resolution window RenderWindow window(VideoMode(640, 640), "Homework 09: Tic-Tac-Toe"); // Draw loop: Each iteration of this loop draws a single frame // Continue to draw until window is closed or game is over while (window.isOpen() && gameOver == 0) { window.clear(); // Erase the screen with black DrawTicTacToeBoard( window ); // draw our tic-tac-toe board /****** BEGIN PART II - PLACE THE PIECES ******/ /****** END PART II ******/ window.display(); // Apply all the draws to the screen Event event; // set up an Event object while (window.pollEvent(event)) { // check for a recent event if (event.type == Event::Closed) { // if this is a close window event window.close(); // close the window } else if( event.type == Event::TextEntered ) { // else if a character was entered from the keyboard if( event.text.unicode < 128 ) { // check if it is an acceptable ASCII value int keyCode = event.text.unicode; // returns ASCII value of character entered /****** BEGIN PART III - GAME PLAY ******/ /****** END PART III ******/ } } } } /****** BEGIN PART IV - PRINT WINNER ******/ /****** END PART IV ******/ return EXIT_SUCCESS; } /***** FUNCTION DEFINITIONS *****/ /* * Check if the given move is a valid move. That is, at the * target row and column position, is the game board blank? * * Parameters: * char board[3][3] : 2D character array representing Tic-Tac-Toe board * int row : matrix row location * int col : matrix column location * Returns: * bool : True if valid move, False if invalid */ bool IsMoveValid( char board[3][3], int row, int col ) { return false; } /* * Check if the game is over, and if it is over how did the game end? * This function can return four different values representing the * current game state: * 0 - game is not over * 1 - game is over, X won * 2 - game is over, O won * 3 - game is over, draw * * Parameters: * char board[3][3] : 2D character array representing Tic-Tac-Toe board * Returns: * int : integer representing game state */ int IsGameOver( char board[3][3] ) { return 0; } /* * Automatically play for the computer player. The computer * randomly places an 'O' at an open position. * * Parameters: * char board[3][3 : 2D character array representing Tic-Tac-Toe board */ void ComputerMove( char board[3][3] ) { int compRow, compCol; do { compRow = rand() % 3; // select a random row compCol = rand() % 3; // select a random column } while( !IsMoveValid(board, compRow, compCol) ); // until this position is valid board[compRow][compCol] = 'o'; // place an 'O' here } /* * Draws our Tic-Tac-Toe board. * We must pass our RenderWindow by reference because the * RenderWindow class does not have a copy constructor so * we cannot pass by value. * * Parameters: * RenderWindow &win : The window to draw into */ void DrawTicTacToeBoard( RenderWindow &win ) { RectangleShape topBar; topBar.setFillColor( Color::White ); topBar.setPosition( Vector2f( 15, 213 ) ); topBar.setSize( Vector2f( 610, 15 ) ); RectangleShape botBar; botBar.setFillColor( Color::White ); botBar.setPosition( Vector2f( 15, 426 ) ); botBar.setSize( Vector2f( 610, 15 ) ); RectangleShape leftBar; leftBar.setFillColor( Color::White ); leftBar.setPosition( Vector2f( 213, 15 ) ); leftBar.setSize( Vector2f( 15, 610 ) ); RectangleShape rightBar; rightBar.setFillColor( Color::White ); rightBar.setPosition( Vector2f( 426, 15 ) ); rightBar.setSize( Vector2f( 15, 610 ) ); win.draw( topBar ); win.draw( botBar ); win.draw( rightBar ); win.draw( leftBar ); } /* * Draws an 'O' to the window at a given (row, col) position. * We must pass our RenderWindow by reference because the * RenderWindow class does not have a copy constructor so * we cannot pass by value. * * Parameters: * RenderWindow &win : The window to draw into * int row : 0-indexed row location * int col : 0-indexed col location */ void DrawO( RenderWindow &win, int row, int col ) { CircleShape outerRing; outerRing.setRadius( 92 ); outerRing.setPosition( Vector2f( col * 199 + (col+1) * 15 + 7, row * 199 + (row+1) * 15 + 7 ) ); outerRing.setFillColor( Color::Red ); CircleShape innerRing; innerRing.setRadius( 85 ); innerRing.setPosition( Vector2f( col * 199 + (col+1) * 15 + 15, row * 199 + (row+1) * 15 + 15 ) ); innerRing.setFillColor( Color::Black ); win.draw( outerRing ); win.draw( innerRing ); } /* * Draws an 'X' to the window at a given (row, col) position. * We must pass our RenderWindow by reference because the * RenderWindow class does not have a copy constructor so * we cannot pass by value. * * Parameters: * RenderWindow &win : The window to draw into * int row : 0-indexed row location * int col : 0-indexed col location */ void DrawX( RenderWindow &win, int row, int col ) { RectangleShape leftSlash; leftSlash.setFillColor( Color::Blue ); leftSlash.setPosition( Vector2f( col * 199 + (col+1) * 15 + 33, row * 199 + (row+1) * 15 + 33 ) ); leftSlash.setSize( Vector2f( 15, 184 ) ); leftSlash.setRotation( -45 ); RectangleShape rightSlash; rightSlash.setFillColor( Color::Blue ); rightSlash.setPosition( Vector2f( col * 199 + (col+1) * 15 + 163, row * 199 + (row+1) * 15 + 23 ) ); rightSlash.setSize( Vector2f( 15, 184 ) ); rightSlash.setRotation( 45 ); win.draw( leftSlash ); win.draw( rightSlash ); }