Mar 8, 2016 at 2:37pm UTC
Hey,
I'm trying to make a Tic-Tac-Toe program, but I want to keep it as short and as simple as possible..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "stdafx.h"
#include <iostream>
int main(){
int boxes[9]{1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << "|" << " " << boxes[0] << " " << "|" << " " << boxes[1] << " " << "|" << " " << boxes[2] << std::endl;
std::cout << "------------------" << std::endl;
std::cout << "|" << " " << boxes[3] << " " << "|" << " " << boxes[4] << " " << "|" << " " << boxes[5] << std::endl;
std::cout << "------------------" << std::endl;
std::cout << "|" << " " << boxes[6] << " " << "|" << " " << boxes[7] << " " << "|" << " " << boxes[8] << std::endl;
int playerTurn;
int playerChoice;
char Mark;
playerTurn = 1;
std::cin >> playerChoice;
while (playerTurn <= 9){
if (playerTurn == 1 || playerTurn == 3 || playerTurn == 5){
Mark = 'X' ;
}
else {
Mark = 'Y' ;
}
if (playerChoice == boxes[playerChoice - 1]){
boxes[playerChoice - 1] = Mark;
}
playerTurn++;
}
}
Could you help me with this code? And the reason why it isn't working?
Last edited on Mar 8, 2016 at 2:47pm UTC
Mar 8, 2016 at 2:52pm UTC
Ok,
The problem is that you initialized playerTurn to 1 and you are using the while loop while(playerTurn <= 9) which will never be true.
Hope that helps!
Last edited on Mar 8, 2016 at 2:53pm UTC
Mar 8, 2016 at 3:08pm UTC
I changed while to while(playerTurn=1) but it still isn't working :/
Mar 8, 2016 at 3:11pm UTC
Move line 22 inside of the while loop (after lin 23). Do the same with line 7 to 11.
Change the type of boxes
-> char boxes[9]{'1' , ... }; // Note: ''
Remove line 31.
Add if(...) for an invalid user input.
You may use instead of while(...) -> for(...)
Mar 8, 2016 at 3:33pm UTC
Thanks so much! But it now displays the big box every time, can I change that so the numbers just change to marks in the same big box?