Here is the code I have written so far. My problem is that when the second user enters its position the last user's position is wiped of the board. I want to know how I can hold that position and keep doing so until the game is finished. I thought that calling the previous function would do that (and you can see where I have put that into a comment) but it doesn't. thanks
Code:
#include <iostream> //includes header file
using namespace std;
//function prototypes
void printLeftUpper(int i, int j);
void printMiddleUpper(int i, int j);
//delcares enumaration
enum NaughtsCrossesSquare {
leftUpper,
middleUpper,
rightUpper,
leftMiddle,
middle,
rightMiddle,
leftLower,
middleLower,
rightLower,
};
//start of main body of fcn
int main()
{
int i=0; //delcares and initialises integers
int NC;
int playerTurn;
//prints out tic tac toe square
cout << leftUpper << "|" << middleUpper << "|" << rightUpper << endl;
cout << "-----" << endl;
cout << leftMiddle << "|" << middle << "|" << rightMiddle << endl;
cout << "-----" << endl;
cout << leftLower << "|" << middleLower << "|" << rightLower << endl;
cout << endl;
//Expalnes games dynamics
cout << "Two players to play naughts and crosses \n" << endl;
cout << "Player one is X's, Player two is O's" << endl;
while(i<9) //start of while loop incremneting to 9 by 1 each time
{
cout << "Player one enter 1 or player two enter 2 to play" << endl; //gets players to confirm which user is playing
cin >> playerTurn;
cout << "Please select from table which position you would like" << endl; //prompts user for input and stores it
cin >> NC;
switch (NC) //start of swtich fcn
{
case leftUpper: //determines which case selected and calls fcn
cout << "You have selected leftUpper" << endl;
printLeftUpper(playerTurn,NC);
//break;
case middleUpper:
cout << "You have selected middleUpper" << endl;
printMiddleUpper(playerTurn,NC);
break;
case rightUpper:
cout << "You have selected rightUpper" << endl;
break;
case leftMiddle:
cout << "You have selected leftMiddle" << endl;
break;
case middle:
cout << "You have selected middle" << endl;
break;
case rightMiddle:
cout << "You have selected rightMiddle" << endl;
break;
case leftLower:
cout << "You have selected leftLower" << endl;
break;
case middleLower:
cout << "You have selected middleLower" << endl;
break;
case rightLower:
cout << "You have selected rightLower" << endl;
break;
}
i++;
}
}
void printMiddleUpper (int i,int j) //start of fcn
{
char playerMark1 = 'X';
char playerMark2 = 'O';
if (i==1&&j==1) //if first player move puts a X in place
{
cout << leftUpper << "|" << playerMark1 << "|" << rightUpper << endl;
cout << "-----" << endl;
cout << leftMiddle << "|" << middle << "|" << rightMiddle << endl;
cout << "-----" << endl;
cout << leftLower << "|" << middleLower << "|" << rightLower << endl;
cout << endl;
}
else if (i==2&&j==1) //if second players move puts a O in place
{
cout << leftUpper << "|" << playerMark2 << "|" << rightUpper << endl;
cout << "-----" << endl;
cout << leftMiddle << "|" << middle << "|" << rightMiddle << endl;
cout << "-----" << endl;
cout << leftLower << "|" << middleLower << "|" << rightLower << endl;
cout << endl; ;
}
//printLeftUpper(i,j); //thought that this would hold previous result
}
void printLeftUpper (int i,int j) //start of fcn
{
char playerMark1 = 'X';
char playerMark2 = 'O';
if (i==1&&j==0) //if first player move puts a X in place
{
cout << playerMark1 << "|" << middleUpper << "|" << rightUpper << endl;
cout << "-----" << endl;
cout << leftMiddle << "|" << middle << "|" << rightMiddle << endl;
cout << "-----" << endl;
cout << leftLower << "|" << middleLower << "|" << rightLower << endl;
cout << endl;
}
else if (i==2&&j==0) //if second players move puts a O in place
{
cout << playerMark2 << "|" << middleUpper << "|" << rightUpper << endl;
cout << "-----" << endl;
cout << leftMiddle << "|" << middle << "|" << rightMiddle << endl;
cout << "-----" << endl;
cout << leftLower << "|" << middleLower << "|" << rightLower << endl;
cout << endl;
}
}