230 likes | 404 Views
Tic Tac Toe. By Dave Florek. Project. Came up with a two player versus mode which is partially derived from a player vs. cpu Encountered problems with player vs. cpu mode. What is Tic Tac Toe . Game that uses an enlarged number sign with two players, one X, the other O.
E N D
Tic Tac Toe By Dave Florek
Project • Came up with a two player versus mode which is partially derived from a player vs. cpu • Encountered problems with player vs. cpu mode
What is Tic Tac Toe • Game that uses an enlarged number sign with two players, one X, the other O. • Objective is to try and get three 1’s or 2’s based on player# in a row inside the entire box to win and to stop the other player from making theirs in. • The first person to do this wins.
How it works Player 1 Array Player 2 Array A coin is tossed that has a 50-50 shot of choosing which player goes first. Next, the player, using the keypad, inputs what spot he wants. Then, the other player goes first and alternates back and forth until one of the players win while the program checks if the moves are valid and if the player wins, then it quits.The moves made by both arrays are stored in a storage array that I will explain later.
Project files • There are two header files • playerVS.h • Tictactoe.h • Three source files • Player.cpp • Tictactoe.cpp • Main.cpp
playerVS.h • #include "tictactoe.h"//Load the base class • class playerVS : public TicTacToe//Base class is TicTacToe with playerVS as the derived class • { • public: • playerVS(int, int);//constructor • void moveVS(int); • void menuVS(); • void player1(int [3][3]); • void player2(int [3][3]); • void win(int); • private: • //TicTacToe object here • int p[3][3]; • };
playerVS.cpp • #include<iostream> • using std::cin; • using std::cout; • using std::endl; • #include "playervs.h" • playerVS::playerVS(int value1, int value2) • :TicTacToe(value1)//playerVS constructor here • { • for(int i=0;i<=2;i++) • { • for(int j=0;j<=2;j++) • { • p[i][j]=value2; • } • } • } • void playerVS::moveVS(int coinToss)//Invoke the coin toss and decide who moves first • { • int move=0, moveA[3][3]; • for(int i=0;i<=2;i++)//Set moveArray to zero • { • for(int j=0;j<=2;j++) • { • moveA[i][j]=0; • } • } • if(coinToss==1) • player1(moveA); • if(coinToss==2) • player2(moveA); • }
playerVS.cpp • void playerVS::menuVS()//Invoke the coin toss and tell who will move first • { • int coin=randCoinToss(); • moveVS(coin); • } • void playerVS::player1(int moveTemp[3][3]) • { • int move1=0; • cout<<"Player 1, your move."<<endl; • cin>>move1; • int moveArray[3][3]; • for(int i=0;i<=2;i++) • { • for(int j=0;j<=2;j++) • { • moveArray[i][j]=0; • } • } • //Player 1 moves • if(move1==1) • moveArray[2][0]=1; • else if(move1==2) • moveArray[2][1]=1; • else if(move1==3) • moveArray[2][2]=1; • else if(move1==4) • moveArray[1][0]=1; • else if(move1==5) • moveArray[1][1]=1; • else if(move1==6) • moveArray[1][2]=1; • else if(move1==7) • moveArray[0][0]=1; • else if(move1==8) • moveArray[0][1]=1; • else if(move1==9) • moveArray[0][2]=1;
playerVS.cpp • printStatus(p);//print array • system("pause"); • //Check Moves to see if they are true=ok or false=no • for(int a=0;a<=2;a++)//check the move to make sure it is valid cause the original arrays are all 0 except the 1 and 2 • { • for(int b=0;b<=2;b++) • { • if((moveArray[a][b]!=0)&&(p[a][b]!=0)) • { • cout<<"Error! That spot has been taken! Try again."<<endl; • player1(moveArray); • } • if((moveArray[a][b]!=0)&&(p[a][b]==0)) • p[a][b]=moveArray[a][b]; • } • } • if(checkWin(moveArray)==true)//you won • { • int player1=1;//you won • cout<<"Player 1 has won!"<<endl; • printStatus(p); • system("pause"); • } • player2(moveArray); • }
void playerVS::player2(int moveArray[3][3])//Take in a temporary array • { • for(int i=0;i<=2;i++) • { • for(int j=0;j<=2;j++) • { • moveArray[3][3]=0; • } • } • int move2=0; • cout<<"Player 2, your move."<<endl; • cin>>move2; • if(move2==1) • { • moveArray[2][0]=2; • } • else if(move2==2) • { • moveArray[2][1]=2; • } • else if(move2==3) • { • moveArray[2][2]=2; • } • else if(move2==4) • { • moveArray[1][0]=2; • } • else if(move2==5) • { • moveArray[1][1]=2; • } • else if(move2==6) • { • moveArray[1][2]=2; • } • else if(move2==7) • { • moveArray[0][0]=2; • } • else if(move2==8) • { • moveArray[0][1]=2; • } • else if(move2==9) • { • moveArray[0][2]=2; • }
playerVS.cpp • printStatus(p); • system("pause"); • system("cls"); • //printStatus(); • //checkWin(); • for(int a=0;a<=2;a++)//check the move to make sure it is valid cause the original arrays are all 0 except the 1 and 2 • { • for(int b=0;b<=2;b++) • { • if((moveArray[a][b]!=0)&&(p[a][b]!=0)) • { • cout<<"Error! That spot has been taken! Try again."<<endl; • player2(moveArray); • } • if((moveArray[a][b]!=0)&&(p[a][b]==0)) • p[a][b]=moveArray[a][b]; • } • } • if(checkWin(moveArray)==true) • { • int player2=2;//you won • cout<<"Player 2 has won!"<<endl; • printStatus(p); • system("pause"); • } • player1(moveArray); }//end of file
Tictactoe.h • #ifndef TICTACTOE_H • #define TICTACTOE_H • class TicTacToe { • public: • TicTacToe(int); • //Menu related definitions • void menu(); • void credits() { cout<<"Created by Dave Florek (C) 2006.\n"<<endl; menu();} • void howToPlay() { cout<<"This is a Tic-Tac-Toe game. The purpose is to obtain three O's or X's in the"<<"\nspaces using the keyboard's keypad." • <<" For example, by pressing the key 7, you"<<"\nwill create an X or O based on the random coin toss and place it the top left"<<"\nbox.\n"<<endl; menu();} • void error() {cout<<"Error! Invalid entry!\n"<<endl; menu();} • //Begin the program here • int randCoinToss(); • void start(); • //Level 1 definition • void level1(); • //Level 2 definitions • void level2(int); • void thinkAhead(); • void level2Moves(); • //Level 3 definitions • void level3(int); • void stalemate(); • void level3Moves(); • bool checkWin(int [3][3]); • bool checkMove(int [3][3]); • void printStatus(int [3][3]); • void move(int,int); • private: • int t[3][3]; }; • #endif
TicTacToe.cpp • This is the unfinished AI part of the program. • There are three functions that we referenced here in the base class: randCoinToss(), checkWin(), printStatus()
TicTacToe.cpp • //The TicTacToe function members defined here • #include <iostream> • using std::cin; • using std::cout; • using std::endl; • #include <stdlib.h> • #include<time.h> • #include "tictactoe.h" //TicTacToe definition header • TicTacToe::TicTacToe(int value1) • { • for(int i=0;i<=2;i++) • { • for(int j=0;j<=2;j++) • { • t[i][j]=value1; • } • } • } • //Begin tictactoe with a random coin toss • int TicTacToe::randCoinToss() • { • srand( (unsigned)time( NULL ) );//initialize random generator • int toss=(1+rand()%2); //make the toss a 50-50 shot of 1 or 2 • return toss; • }
TicTacToe.cpp • //Print store array • void TicTacToe::printStatus(int print[3][3]) • { • cout<<endl; • cout<<print[0][0]<<"|"<<print[0][1]<<"|"<<print[0][2]<<"\n-----\n" • <<print[1][0]<<"|"<<print[1][1]<<"|"<<print[1][2]<<"\n-----\n" • <<print[2][0]<<"|"<<print[2][1]<<"|"<<print[2][2]<<endl; • } • bool TicTacToe::checkWin(int arrayWin[3][3]) • { • //if arrayWin doesn't match any of the patterns • //return false; • if(arrayWin[0][0]==arrayWin[0][1]&&arrayWin[0][1]==arrayWin[0][2]) • return true; • if(arrayWin[1][0]==arrayWin[1][1]&&arrayWin[1][1]==arrayWin[1][2]) • return true; • if(arrayWin[2][0]==arrayWin[2][1]&&arrayWin[2][1]==arrayWin[2][2]) • return true; • if(arrayWin[0][0]==arrayWin[1][0]&&arrayWin[1][0]==arrayWin[2][0]) • return true; • if(arrayWin[0][1]==arrayWin[1][1]&&arrayWin[1][1]==arrayWin[2][1]) • return true; • if(arrayWin[0][2]==arrayWin[1][2]&&arrayWin[1][2]==arrayWin[2][2]) • return true; • if(arrayWin[0][0]==arrayWin[1][1]&&arrayWin[1][1]==arrayWin[2][2]) • return true; • if(arrayWin[0][2]==arrayWin[1][1]&&arrayWin[1][1]==arrayWin[2][0]) • return true; • //if arrayWin does match the patterns • else • return false; • }
Main.cpp • //main file • #include <iostream> • using std::cin; • using std::cout; • using std::endl; • #include <stdlib.h> • #include<time.h> • #include "tictactoe.h" • #include "playervs.h" • int main() • { • int select, z=0; • TicTacToe player1(z); • playerVS player2(z,z); • cout<<"\n========================="<<endl; • cout<<"\n= ="<<endl; • cout<<"\n= Welcome to TicTacToe! ="<<endl; • cout<<"\n= ="<<endl; • cout<<"\n========================="<<endl; • system("pause"); • system("cls"); • cout<<"Main Menu"<<endl; • cout<<"1) Player vs Player 2"<<endl; • cout<<"2) Player vs CPU"<<endl; • cin>>select; • if(select==2) • player1.menu(); • if(select==1) • player2.menuVS(); • return 0;}
Minor problems in code • There is a delay in the printStatus() function that causes the screen to print an entire blank screen after user input. But it prints the user input at the next printStatus() call. • After you are flagged by the error message that that spot is taken, if you move again, it will not show that move at all. • After a player wins, the code keeps continuing. After attempts to break, I get a compile error with an illegal break.
Future improvements • Fix the printStatus() function so that it prints the value from the moveArray that doesn’t appear in the p (Array). • Make the Player vs CPU work properly. • Create a GUI for simplification that still relies on the keypad for input instead of a mouse. • Create a scoring system with the AI using an external file to store the results.