PLEASE help! C++ assignment due tonight!Computer Tick Tack Toe


The problem I am having is the computer does not switch turns after it inputs a move, infact the computer endlessly inputs the move.


//This program is a tic tac toe program
//include the following libraries
#include "stdafx.h"
#include<iostream>
#include<string>
#include<ctime>

//use standard namespace
using namespace std;

//declare global variables
char board[9];
//declare functions

void showboard();
bool moveisvalid(int m);
int whowon();//returns 0 if no one won, 1 if player 1 won, 2 if player 2 won

void main ()
{
//declare local variables
string player_name_1;
srand(time (NULL));
int player_name_2=rand()% 8+1;
int whose_turn=1; //this variable states that it is player 1's turn, if the value is set to 2 it is player 2's turn
int move;//this variable stores the players position as a result of the last move
int totalmoves=0;//keaps trac of the total amount of moves

//assign values to array slots
board[0]='0';
board[1]='1';
board[2]='2';
board[3]='3';
board[4]='4';
board[5]='5';
board[6]='6';
board[7]='7';
board[8]='8';

//tell players to input their names
cout<<"Player 1, please enter your name"<<endl;
cin>>player_name_1;

//call the board outside main
//run this function untill the player chooses a valid move
while(whowon()==0 && totalmoves<9)
{
do
{
showboard();
//tells which player move it is
if(whose_turn==1)
{
cout<< player_name_1<<" make a move."<<endl;
cin>>move;
}
else
{
player_name_2;
}
//get the players position of the move he inputed
}

while(moveisvalid (move) !=true);
//add 1 to the total amount of moves every time a move is valid
totalmoves++;
//change whose turn it is
switch(whose_turn)
{
case(1):
{
board[move]='x';

whose_turn=2;
break;

}
case(2):
{
board[player_name_2]='o';
whose_turn=1;

}
}
}
//show the board
showboard();

if ( whowon()==1)
{
cout<<player_name_1<<" you have won the game"<<endl;

}
else if(whowon()==2)
{
cout<<"Computer has won"<<endl;

}
else
{
cout<<"A tie game occured."<<endl;
}
system("PAUSE");
}

//use declared functions
void showboard()
{
//show the board on the command promt screen
cout<<endl;
cout<<"|"<<board[0]<<"|"<<board[1]<<"|"<<board[2]<<"|"<<endl;
cout<<"--+---"<<endl;
cout<<"|"<<board[3]<<"|"<<board[4]<<"|"<<board[5]<<"|"<<endl;
cout<<"--+---"<<endl;
cout<<"|"<<board[6]<<"|"<<board[7]<<"|"<<board[8]<<"|"<<endl;
cout<<endl;
}

bool moveisvalid(int m)
{
if(board[m] !='x' && board[m] !='o')
{
return true;
}
else
{
return false;
}
}

int whowon()
{
if(board[0]==board[1] && board[1]==board[2])
{
if(board[0]=='x')
{
return 1;
}
else
{
return 2;
}

}

if(board[3]==board[4] && board[3]==board[5])
{
if(board[3]=='x')
{
return 1;
}
else
{
return 2;
}

}
if(board[6]==board[7] && board[6]==board[8])
{
if(board[6]=='x')
{
return 1;
}
else
{
return 2;
}

}
if(board[0]==board[3] && board[0]==board[6])
{
if(board[0]=='x')
{
return 1;
}
else
{
return 2;
}

}
if(board[1]==board[4] && board[1]==board[7])
{
if(board[1]=='x')
{
return 1;
}
else
{
return 2;
}

}
if(board[2]==board[5] && board[2]==board[8])
{
if(board[2]=='x')
{
return 1;
}
else
{
return 2;
}
}

if(board[0]==board[4] && board[0]==board[8])
{
if(board[0]=='x')
{
return 1;
}
else
{
return 2;
}

}

if(board[2]==board[4] && board[2]==board[6])
{
if(board[2]=='x')
{
return 1;
}
else
{
return 2;
}

}


return 0;
}
You are just reading the move from the first player. Let's say I enter 2 for my move. It's gonna display x in position 2 and then just keep on displaying the board over and over, because the other player is not making move!

consider revising this part
1
2
3
4
5
6
7
8
9
10
11
if(whose_turn==1)
{
     cout<< player_name_1<<" make a move."<<endl;
     cin>>move;
}
else
{
      player_name_2;
}
//get the players position of the move he inputed
} 
Topic archived. No new replies allowed.