Help! TicTacToe Who Goes First issue

This code for a game of tic tac toe would work fine...except it won't pick who goes first and just go with it! It goes to the very top of the code again and changes the variable that needs to stay the same. If this variable could be unchanged, this would be working! Help me I'm new to the language.


#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
//int func1;
int func2;
int func3;
int func4;
int x;


char block[10] = {'o','1','2','3','4','5','6','7','8','9'};
int checkwin();
void tictactoe();

int main()
{
//The problem is the program goes through the whole code again and is willing to
//go to either case in the switch because the random will change each through.
srand ( time(NULL) );
int turn = 1,i,choice;
char mark;
int first = rand() % 2 + 1;
static int now = first;

switch (now){
case 1:

do
{
tictactoe();
turn=(turn%2)?1:2;
cout << "Player " << turn << ", enter a number: ";

if (turn == 1){
cin >> choice;
}
if (turn == 2){
int move = rand() % 9 + 1;
choice = move;
}
mark=(turn == 1) ? 'X' : 'O';
if (choice == 1 && block[1] == '1'){
block[1] = mark;
}
else if
(choice == 2 && block[2] == '2'){
block[2] = mark;
}
else if
(choice == 3 && block[3] == '3'){
block[3] = mark;
}
else if

(choice == 4 && block[4] == '4'){
block[4] = mark;
}
else if
(choice == 5 && block[5] == '5'){
block[5] = mark;
}
else if
(choice == 6 && block[6] == '6'){
block[6] = mark;
}
else if
(choice == 7 && block[7] == '7'){
block[7] = mark;
}
else if (choice == 8 && block[8] == '8'){
block[8] = mark;
}
else if (choice == 9 && block[9] == '9'){
block[9] = mark;
}
else
{
cout<<"\nINVALID MOVE ";
turn--;
}
i=checkwin();
turn++;
}
while(i==-1);
tictactoe();
if(i==1){
cout<<"==>\aPlayer "<<--turn<<" win ";
cin.get();
cin.get();
}

else{
cout<<"==>\aGame draw";
return 0;
cin.get();
}
break;
case 2:
do
{
tictactoe();
turn=(turn%2)?2:1;
cout << "Player " << turn << ", enter a number: ";

if (turn == 1){
cin >> choice;
}
if (turn == 2){
int move = rand() % 9 + 1;
choice = move;
}
mark=(turn == 1) ? 'X' : 'O';
if (choice == 1 && block[1] == '1'){
block[1] = mark;
}
else if
(choice == 2 && block[2] == '2'){
block[2] = mark;
}
else if
(choice == 3 && block[3] == '3'){
block[3] = mark;
}
else if

(choice == 4 && block[4] == '4'){
block[4] = mark;
}
else if
(choice == 5 && block[5] == '5'){
block[5] = mark;
}
else if
(choice == 6 && block[6] == '6'){
block[6] = mark;
}
else if
(choice == 7 && block[7] == '7'){
block[7] = mark;
}
else if (choice == 8 && block[8] == '8'){
block[8] = mark;
}
else if (choice == 9 && block[9] == '9'){
block[9] = mark;
}
else
{
cout<<"\nINVALID MOVE ";
turn--;
}
i=checkwin();
turn++;
}
while(i==-1);
tictactoe();
if(i==1){
cout<<"==>\aPlayer "<<--turn<<" wins! ";
cin.get();
cin.get();
}

else{
cout<<"==>\aGame draw";
return 0;
cin.get();
}
break;
default:
cout<<"Sorry Have to quit. Restart program";cin.get();
break;

}
}



int checkwin()
{
if (block[1] == block[2] && block[2] == block[3]){
return 1;
}
else if (block[4] == block[5] && block[5] == block[6]){
return 1;
}
else if (block[7] == block[8] && block[8] == block[9]){
return 1;
}
else if (block[1] == block[4] && block[4] == block[7]){
return 1;
}
else if (block[2] == block[5] && block[5] == block[8]){
return 1;
}
else if (block[3] == block[6] && block[6] == block[9]){
return 1;
}
else if (block[1] == block[5] && block[5] == block[9]){
return 1;
}
else if (block[3] == block[5] && block[5] == block[7]){
return 1;
}
else if (block[1] != '1' && block[2] != '2' && block[3] != '3' && block[4] != '4'
&& block[5] != '5' && block[6] != '6' && block[7] != '7' && block[8] != '8' && block[9] != '9'){
return 0;
}
else{
return -1;
}
}



void tictactoe()
{

cout << "\n\n\tTic-Tac-Toe\n\n";
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << " | | " << endl;
cout << " " << block[7] << " | " << block[8] << " | " << block[9] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << block[4] << " | " << block[5] << " | " << block[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << block[1] << " | " << block[2] << " | " << block[3] << endl;
cout << " | | " << endl << endl;
}
hey idk about this but i just read your last line about you needing it unchanged. Try adding the keyword "const" before you declare the type.
Like const int x;
This makes it constant and unchangeable.
Thanks that seemed to help!

I'm having another error that I don't understand though.
If I am chosen to go first, the program works fine.
If the AI goes first, (Player 2), it sometimes decides to take more than one turn before it is my turn. Any suggestions?
Topic archived. No new replies allowed.