Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Tic Tac Toe By Dave Florek

2 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

3 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.

4 How it works X X Player 1 Array Player 2 Array O A coin is tossed that has a 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.

5 Project files There are two header files –playerVS.h –Tictactoe.h Three source files –Player.cpp –Tictactoe.cpp –Main.cpp

6 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]; };

7 playerVS.cpp #include 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); }

8 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."<>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;

9 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."<

10 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."<>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; }

11 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."<

12 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"<

13 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()

14 TicTacToe.cpp //The TicTacToe function members defined here #include using std::cin; using std::cout; using std::endl; #include #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 shot of 1 or 2 return toss; }

15 TicTacToe.cpp //Print store array void TicTacToe::printStatus(int print[3][3]) { cout<

16 Main.cpp //main file #include using std::cin; using std::cout; using std::endl; #include #include "tictactoe.h" #include "playervs.h" int main() { int select, z=0; TicTacToe player1(z); playerVS player2(z,z); cout<<"\n========================="<>select; if(select==2) player1.menu(); if(select==1) player2.menuVS(); return 0;}

17 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.

18 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.


Download ppt "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."

Similar presentations


Ads by Google