Tic tac toe

DELETED.
Last edited on
well, start by making a board and initialize it and clear it, get those parts working. Build a little piece, test it, debug it, then add another piece... next try place mark on board ... get it working ...
why this way? You want to do the things that let you debug the others first. You can't do game logic until you can place markers and see the board, so you do that first...
It does not have to be in that specific order, just needs to use all 8 of them
i found this old tic tac toe program in my pc, i programmed it 2014.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
using namespace std;

void diagram(char ar[]){		
cout<< ar[6] << " " << ar[7] << " " << ar[8] << "\n"
	<< ar[3] << " " << ar[4] << " " << ar[5] << "\n"
	<< ar[0] << " " << ar[1] << " " << ar[2] << "\n";
return;		
}

int check(char ar[], char p,  int count){
if ( 
		(ar[0]==p && ar[1]==p && ar[2]==p) ||  
		(ar[3]==p && ar[4]==p && ar[5]==p) ||   
		(ar[6]==p && ar[7]==p && ar[8]==p) || 
		
		(ar[0]==p && ar[3]==p && ar[6]==p) ||  
		(ar[1]==p && ar[4]==p && ar[7]==p) ||   
		(ar[2]==p && ar[5]==p && ar[8]==p) || 
		
		(ar[0]==p && ar[4]==p && ar[8]==p) ||   
		(ar[2]==p && ar[4]==p && ar[6]==p)
   )
	   
    return 10;  // if anyplayer wins.

else
    return ++count; //count == nos valid move played
}


int main() {
	system("color CE"); //default 07
	char ar[9] = {'1','2','3','4','5','6','7','8','9'};
	int p, count=0;

	cout<< "input position from numpad\n" 	
		<< "7 8 9\n"
		<< "4 5 6\n"
		<< "1 2 3\n\n";
		
while(true) {
	if(count<9)	{
		X:
		cout<<"enter position for X. ";
		cin >> p;	
		bool bx=false;
		for(int i=0; i<9; i++) {
			if(ar[i]==p+48) {  // ASCII value
				ar[i] = 'X';
				bx= true;
			}
		}
		
		if(!bx) {
			cout << "invalid input, try again\n";
			goto X;	
		}
		
		diagram(ar);
		count = check(ar, 'X', count);
		if(count==10) { 
			cout <<"\nX Wins.\n"; break; 
		}
	}

	if(count<9)	{
		O:
		cout<<"enter position for O. ";
		cin >> p;	
		bool bo=false;
		for(int i=0; i<9; i++) {
			if(ar[i]==p+48) {
				ar[i] = 'O';
				bo=true;
			}				
		}	
		if(!bo) {
			cout << "invalid input, try again\n";
			goto O;	
		}
			
		diagram(ar);
		count = check(ar, 'O', count);
		if(count==10) { 
			cout <<"\nY Wins.\n"; break; 
		}
	}	

	
	if (count == 9)	
		{ cout <<"\nGame Draw.\n";  break; }	  		  
}	

return 0;		
}


output:
input position from numpad
7 8 9
4 5 6
1 2 3

enter position for X. 5
7 8 9
4 X 6
1 2 3
enter position for O. 4
7 8 9
O X 6
1 2 3
enter position for X. 9
7 8 X
O X 6
1 2 3
enter position for O. 1
7 8 X
O X 6
O 2 3
enter position for X. 7
X 8 X
O X 6
O 2 3
enter position for O. 8
X O X
O X 6
O 2 3
enter position for X. 3
X O X
O X 6
O 2 X

X Wins.
here's one I prepared earlier (about 8 years..):

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <unordered_map>
#include <iostream>
#include <string>
using namespace std;

struct point
{
	int r;
	int c;

	point(int rr, int cc) : r(rr), c(cc) {}
};

const string lft[3] {"T", "M", "B"};
const string top[3] {"L", "C", "R"};

const char sym[2] {'X', 'O'};

const unordered_map<string, point> msp {make_pair(lft[0] + top[0], point(0, 0)),
					make_pair(lft[0] + top[1], point(0, 1)),
					make_pair(lft[0] + top[2], point(0, 2)),
					make_pair(lft[1] + top[0], point(1, 0)),
					make_pair(lft[1] + top[1], point(1, 1)),
					make_pair(lft[1] + top[2], point(1, 2)),
					make_pair(lft[2] + top[0], point(2, 0)),
					make_pair(lft[2] + top[1], point(2, 1)),
					make_pair(lft[2] + top[2], point(2, 2))};

const point win[8][3] {{ { 0, 0 }, { 0, 1 }, { 0, 2 } },
			{ { 1, 0 }, { 1, 1 }, { 1, 2 } },
			{ { 2, 0 }, { 2, 1 }, { 2, 2 } },
			{ { 0, 0 }, { 1, 0 }, { 2, 0 } },
			{ { 0, 1 }, { 1, 1 }, { 2, 1 } },
			{ { 0, 2 }, { 1, 2 }, { 2, 2 } },
			{ { 0, 0 }, { 1, 1 }, { 2, 2 } },
			{ { 0, 2 }, { 1, 1 }, { 2, 0 } }};

string toupper(const string& st)
{
	string tu;

	for (auto ch : st)
		tu += toupper(ch);

	return tu;
}

void draw(char board[3][3])
{
	cout << endl;
	for (int t = 0; t < 3; ++t)
		cout << "   " << top[t];

	cout << endl << endl;

	for (int rr = 0; rr < 3; ++rr) {
		if (rr > 0)
			cout << "  -----------" << endl;

		for (int cc = 0; cc < 3; ++cc) {
			(cc > 0) ? cout << " |" : cout << lft[rr] << " ";
			cout << " " << board[rr][cc];
		}
		cout << endl;
	}
}

int main()
{
	char again = 'n';

	do {
		char board[3][3] = {0};
		string inp;
		auto mit = msp.cbegin();
		char gotwin = ' ';

		draw(board);

		for (int cnt = 0, play = 0; (cnt < 9) && (gotwin == ' ');) {
			while ((cout << "\nEnter location for player (RC)" << play + 1 << " [" << sym[play] << "]: ") && (cin >> inp) && ((mit = msp.find(toupper(inp))) == msp.end()))
				cout << "Invalid location";

			char& pos = board[mit->second.r][mit->second.c];

			if (pos > ' ')
				cout << "Space taken!" << endl;
			else {
				pos = sym[play];
				++cnt;
				play = (play + 1) % 2;
				draw(board);

				for (int w = 0, b; (w < 8) && (gotwin == ' '); ++w)
					if (((b = board[win[w][0].r][win[w][0].c]) > ' ') && (b == board[win[w][1].r][win[w][1].c]) && (b == board[win[w][2].r][win[w][2].c]))
						gotwin = b;

				if (gotwin != ' ')
					cout << "\nPlayer " << (gotwin == 'X' ? "1" : "2") << " wins!" << endl;
			}
		}

		if (gotwin == ' ')
			cout << "\nDraw!" << endl;

		while ((cout << "\nPlay again? [Y, N]: ") && (cin >> again) && ((again = toupper(again)) != 'N' && (again != 'Y')))
			cout << "Please enter 'Y' or 'N': ";

	} while (again == 'Y');
}




   L   C   R

T    |   |
  -----------
M    |   |
  -----------
B    |   |

Enter location for player 1 [X]: tl

   L   C   R

T  X |   |
  -----------
M    |   |
  -----------
B    |   |

Enter location for player 2 [O]: bl

   L   C   R

T  X |   |
  -----------
M    |   |
  -----------
B  O |   |

Enter location for player 1 [X]: mc

   L   C   R

T  X |   |
  -----------
M    | X |
  -----------
B  O |   |

Enter location for player 2 [O]: bc

   L   C   R

T  X |   |
  -----------
M    | X |
  -----------
B  O | O |

Enter location for player 1 [X]: br

   L   C   R

T  X |   |
  -----------
M    | X |
  -----------
B  O | O | X

Player 1 wins!

Play again? [Y, N]: n


These don't use the specified functions though.....

Last edited on
Topic archived. No new replies allowed.