Tic-Tac-Toe program

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
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
I changed while to while(playerTurn=1) but it still isn't working :/
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(...)
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?
You may use std::system("CLS"); before showing the box.

http://www.cplusplus.com/reference/cstdlib/system/?kw=system

Note that its behavor might be system dependent and should usually be avoided.
Topic archived. No new replies allowed.