Function call

Hello,

I`m not sure if I have called function int Search(int depth) correctly on line 167. If that`s the case, it would explain why the program behaves so strangely.

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iostream>
#include <bitset>
#include <cstdlib>
#include <vector>

using std::cin;
using std::cout;
using std::ostream;
using std::endl;
using std::bitset;

const size_t N = 3;
typedef bitset <N*N> board;


const board wins[] = {
    board(0b111000000), board(0b000111000), board(0b000000111),    // rows
    board(0b100100100), board(0b010010010), board(0b001001001),	// cols
    board(0b100010001), board(0b001010100)	// diagonals
};

board noughts;			// bit board for first player
board crosses;			// bit board for second player


const char NOUGHT = 'O';
const char CROSS = 'X';


board combined();
ostream & print_board(ostream & stm = cout, board b = combined());

// This indicates the current player. Computer plays noughts.
bool curr_move_noughts = true;

board
combined()
{
    return noughts | crosses;
}				// combined bit board for both players

bool
valid(size_t row, size_t col)
{
    return row < N && col < N;
}

size_t
pos(size_t row, size_t col)
{
    return row * N + col;
}				// map row, col to bit position

bool
occupied(size_t row, size_t col)
{
    return valid(row, col) && combined()[pos(row, col)];
}

bool
free(size_t row, size_t col)
{
    return valid(row, col) && !occupied(row, col);
}

size_t last_move_row = -1, last_move_col = -1 ;

void make_move( size_t row, size_t col, board & b )
{
    if (free(row, col))
    {
	b[pos(row, col)] = true;
        last_move_row = row ;
        last_move_col = col ;
    } 
}

void unMake(board & b )
{
	
    
    if (valid(last_move_row, last_move_col))// if valid last_move_row, last_move_col
    {
    int row= -1;
    int col= -1;
    b[pos(row, col)] = true;
    last_move_row = -1 ;
    last_move_col = -1 ; 
    }
}

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

bool
is_win(board b)
{
    for (int i = 0; i < sizeof(wins) / sizeof(wins[0]); ++i) {
	if ((wins[i] & b) == wins[i])
	    return true;
    }
    return false;
}

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

// is the board full?
bool is_full(board b)
{
    static board fullboard(0b111111111);
    return b == fullboard;
}

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

int Evaluate()

{ 
    
 int depth;

 if (is_win (crosses))             

 {return depth-1;}

 else if (is_win (curr_move_noughts))

 {return  1-depth; }

 else   {return 0;} 
 
 

}





void
play()
{

    int row = -1;		// For player input
    int col = -1;

    for (int turns = 0; turns < 9; ++turns) {
	curr_move_noughts = !curr_move_noughts;

	if (curr_move_noughts) {
	   int Search(int depth); 		// computer plays noughts
	} else {
	    cout << "Choose a move..." << endl;
	    cout << "Enter row and col : ";
	    cin >> row >> col;
	    // dmh - note that valid rows/col are 0-2, not 1-3. If you
	    // want the user to enter 1-3 then decrement the value
	    // they enter right after they enter it
	    if (!valid(row,col)) {
		cout << "Illegal move!  Try again...\n" << endl;
	    } else {
		make_move(row, col);
	    }
	}
	print_board();
	if (is_win()) {
	    cout << "Player " << (curr_move_noughts ? NOUGHT : CROSS) << " has won!" << endl; exit(0);
	} else if (is_full()) {
	    cout << "Game ended in a tie!" << endl;
	}
    }
}



ostream & print_board(ostream & stm, board b)
{
    stm << "------\n";
    for (std::size_t i = 0; i < N; ++i) {
	for (std::size_t j = 0; j < N; ++j) {
	    const size_t
		k = pos(i, j);
	    if (b[k])
		stm << (noughts[k] ? NOUGHT : CROSS) << ' ';
	    else
		stm << ". ";
	}
	stm << '\n';
    }
    return stm << "------\n";
}

int Search(int depth)
{
  
  board b = combined();
  int score;
  int x,y;
  int bestScore = -2;
  bool bestMove = false;
  if(depth == 0) return Evaluate(); 
  for (int turns = 0; turns < 9; ++turns) {                 // loop over turns
    while (occupied(x,y));
    make_move(x, y);              
    score = -Search(depth-1);       // recursion 
    unMake(b);
    if(score > bestScore) {         
      bestScore = score;
      bestMove = (x,y);
    }
  }
  return  bestScore; 
  return  bestMove;

 }
 

			

int
main()
{
    play();
    return 0;
}



Here was some output in my tests:

Choose a move...

Enter row and col : Illegal move! Try again...


------

. . .

. . .

. . .

------

------

. . .

. . .

. . .

------

Choose a move...

Enter row and col : Illegal move! Try again...
No you have not called the function correctly. You have a function accepting an int as a parameter and returning an int as a result. So a correct way to use this function would be

1
2
3
int my_result = 0;

my_result = Search(5);


Or, in general

 
int my_result = Search(another_int);


I passed a int to the function, then took the result returned by the function and assigned it to an appropriate variable (i.e. an int).
Last edited on
Ok.

If I remember right, I already tried that. Then I get error " Search was not declared in this scope".

Should I do something like

1
2
3
4
5
6
7
8
9

int Search(int depth);

   if (curr_move_noughts)
       {
	   int z = 0;
           z= Search(5);		// computer plays noughts
	} 
	

That's because the function Search is defined after the line in which you call it. So you have two possible solutions:

1. you move the entire function Search before the main
2. you put a prototype before the main and leave the definition of the function where it is. In this way you "announce" that there will be a funcion called Search that accepts a parameter, etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Choose a move...
Enter row and col : 0 2
. . X 
. . . 
. . . 
------
------
. . X 
. . . 
. . . 
------
Choose a move...
Enter row and col : 0 1
------
. X X 
. . . 
. . . 
------
------
. X X 
. . . 
. . . 
------
Choose a move...
Enter row and col : 



EDIT: That didn`t work out like I was expecting...
Last edited on
Topic archived. No new replies allowed.