Tictactoe10000

Here ís my tictactoe so far:

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
 #include <iostream>
#include <matrix.h>

namespace tictactoe

{
    
    constexpr char NOUGHT = 'O' ;
    constexpr char CROSS = 'X' ;
    bool curr_move_noughts = true ; 

   void board()
	
   {
		void init(Matrix<int,2>&a)
               {
                 for (int r=0; r<a.3 ++r)
                 for (int c=0; c<a.3 ++c)
               }
   }


void play()

   {

for (int turns = 0; turns < 9; ++turns) 

{

   char player= (turns % 2 == 0 ? CROSS : NOUGHT);
{

do
	{
		   int row = -1;
		   int col = -1;

	    cout << "\nPlayer " << player << " -- Choose a move..." << endl;
				cout << "Enter row and col (e.g. 1 2): ";
				cin  >> row >> col;
		

bool valid( int row, int col ) { return row<a.slice(0,r) && column<a.slice(0,c); }

bool occupied( int row, int col ) { return valid(row,col) }
   
bool free( int row, int col ) { return valid(row,col) && !occupied(row,col) ; }
   
void make_move( int row, int col, board ) { if( free(row,col) ) return true; }
 
bool checkwin(char player) {

 if (board a.slice(2,0)==player && board a.slice(1,1)==player && board a.slice(0,2)==player)
 
    return true;                                                   

  if (board a.slice(2,0)==player && board a.slice(2,1)==player && board a.slice(2,2)==player)
 
    return true;                                                  

 if (board a.slice(2,0)==player && board a.slice(1,0)==player && board a.slice(0,0)==player)
 
 return        true;                                                                                                

}

bool is_win() { return is_win( curr_move_noughts ? noughts : crosses ) ; }


 

void displayBoard()
{

   void print(const Matrix<int,2>&a)
   {

       for (int r=0; r<a.3 ++r)
       for (int c=0; c<a.3 ++c)
         cout<<a(r,c)<<'\t';

   }






My question is, how can I check the winning lines effectively instead of having to cover all the

possibilities. Also, I have now idea if there is a chance for the code to work anyway. 100% self-written.
Last edited on
You have to just check all the winning possibilities. In pseudocode:
for each row r
check if row r is all the same
for each column c
check if column c is all the same
check the major diagonal
check the minor diagonal

I'd have the function return the mark of the winning player, or ' ' if no one wins. It makes the check easier. For example, if the board is:
char board[3][3];
Then checking the first row is:
1
2
3
if (board[0][0] == board[0][1] && board[0][0] == board[0][2] && board[0][0] != ' ') {
    return board[0][0];
}


The functions that you've defined in the rest of your code look good, but the code itself won't compile.
Actually I can check the winning lines in a more prudent matter. Here is something that I put together:

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

#include <iostream>
#include <matrix.h>

namespace tictactoe

{
    
    constexpr char NOUGHT = 'O' ;
    constexpr char CROSS = 'X' ;
    AVAILABLE = ' '
    bool curr_move_noughts = true ;

   void board()
	
   {
		void init(Matrix<int,2>&a)
               {
                 for (int r=0; r<a.3 ++r)
                 for (int c=0; c<a.3 ++c)
               }
   }


void play()

   {

for (int turns = 0; turns < 9; ++turns) 

{

   char player= (turns % 2 == 0 ? CROSS : NOUGHT);
   bool valid = false;

do
	{
		   int row = -1;
		   int col = -1;

	    cout << "\nPlayer " << player << " -- Choose a move..." << endl;
				cout << "Enter row and col (e.g. 1 2): ";
				cin  >> row >> col;
		

bool valid( int row, int col ) { return row<a.size(r,0) && column<a.size(0,c); }
pos( int row, int col ) { return row*board + col ; }  // Map row,col to board position

if (!valid)
		{
		cout << "Illegal move!  Try again...\n" << endl;
	        }
		   else
		        {
			   row*board+col = player;

			   displayBoard();

                         }

                  
		if (checkwin(player))

 {  cout<< "Player " <<player<< " has won!" <<endl; exit(0); }

 while (!valid) { cout<< "The game ended in a tie!" <<endl; }

 
				
			

bool occupied( int row, int col ) { return valid(row,col) & return board row*board+col != AVAILABLE; }


   
bool free( int row, int col ) { return valid(row,col) && !occupied(row,col) ; }
   
void make_move( int row, int col, board ) { if( free(row,col) ) return true; }

{ if(curr_move_noughts) make_move(row,col,noughts) ; else make_move(row,col,crosses) ; }

 
bool checkwin(char player) {

   for (int r = 0; r<a.size(r,0) ; ++r) 
   { 

      if (board a.slice(r,0)==player && board a.slice(r,1)==player && board a.slice     (r,2)==player)
 
     return true; 

   }  

    for (int c = 0; c<a.size(0,c) ; ++c) 
   {                                                 

  if (board a.slice(0,c)==player && board a.slice(1,c)==player && board a.slice(2,c)==player)
 
    return true;                                                  

   }

  if ((board a.slice(0,0) == player && board a.slice(1,1) == player && board a.slice(2,2) == player) ||  (board a.slice(0,2) == player && board a.slice(1,1) == player && board a.slice(2,0) == player))
		   
	{ return true; }
		
	                                                                                      



bool is_win() { return is_win( curr_move_noughts ? noughts : crosses ) ; }


}

void displayBoard()
{

   void print(const Matrix<int,2>&a)
   {

       for (int r=0; r<a.3 ++r)
       for (int c=0; c<a.3 ++c)
         cout<<a(r,c)<<'\t';

   }


int main()
{
	TicTacToe ttt;

	ttt.newGame();
	ttt.board();
}







Actually my skills don`t allow me to write code easily afresh, but when I see how other people have done

things, I get a bit grasp on the issue.

I have no idea if it should work somehow now :p Hopefully
Last edited on
What data structure will you use to represent the board? All the code that refers to the board seems scattered to me. Figure that out and tidy up the code.
Most likely an array, though it might be good to study vectors in detail at some point (avoiding the problems of fixed size, etc.)

Back to the issue, I can`t do something like

return board row*board+col != AVAILABLE; ?

Should it be row (-1,0) & col (0,-1) != AVAILABLE ?

I could also consider 0 as AVAILABLE instead of ' '
Last edited on
Wait, can`t I use something like

1
2
3
4
5

 int scale(int row, in col) {return row*col}
 
 b=apply(scale,a,player);                                 // b[i]=a[i]*player for each i


The "freestanding" apply() takes a function that produces a result from it`s argument,

apply() then uses that result to initialize the resulting Matrix
Your past two questions can't be answered until you select the representation for the board. I suggest that you keep it simple and do a two-dimensional array:
char board[3][3]; // legal values are 'X', 'O' and ' '
Yes, I could do something like this:

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

enum Player { none, Player_ X= 'X' , Player_0= '0' }   // Elements are by default initialized to 
0, so none==0.

Matrix<Player,2>board(3,3);

int col=-1;
int row=-1;

void init(Matrix<int,2>&a)
               {
                 for (int r=0; r<a.3 ++r)
                 for (int c=0; c<a.3 ++c)
               }


bool checkwin (char player) {

int scale(int row, in col) {return row*col}
 
 if (b=apply(scale,a,player)); {win=true;}                   // Checking horizontally for each r  


int scale(int row, in col) {return row*col}
 
if ( b=apply(scale,player,a));   {win=true;}                                   // Checking vertically



What is left, is checking the diagonals.
What is Matrix<Player,2> at line 5?
What is a.3 supposed to do at lines 12&13?
What are lines 12 and 13 supposed to do? You have a pair of nested loops but nothing inside the inner-most loop.
What is scale() Supposed to do?
I don't understand lines 21 & 26.

I think at this point you need to work on getting code to compile. The process will point out these things and make you think about what the code should be doing.
Topic archived. No new replies allowed.