-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_moves.cpp
More file actions
51 lines (48 loc) · 1.17 KB
/
random_moves.cpp
File metadata and controls
51 lines (48 loc) · 1.17 KB
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
#pragma once
#include "table.hpp"
/**
* @brief Cpu tries all the squares,
* finds all the legal moves and stores the moves in a vector,
* chooses a random move from the vector and returns it.
*
* @param nothing
*
* @return the coordinates cpu choose
*/
coor Table::randomMoves()
{
int random;
vector<vector<int>> moves;
for (int row = 0; row < SIZE; row++) // tries all the squares on the board if they are legal
{
for (int col = 0; col < SIZE; col++)
{
// if they are legal, they are saved
if (getBoard({row, col}) == LEGAL)
moves.push_back({row, col});
}
}
srand(time(NULL));
random = rand() % moves.size(); // generates random numbers [0, count)
coor c;
c.row = moves[random][0];
c.col = moves[random][1];
return c;
}
/**
* @brief the parameter delay, delays in miliseconds
*
* @param delayInMs
*
* @return coor
*/
coor Table::cpuPlays(unsigned delayInMs)
{
coor c(randomMoves());
sleep(delayInMs); // delays in second
// for a better game exp, prints cpu's moves
cout << "row: " << c.row + 1 << endl;
cout << "col: " << c.col + 1 << endl;
sleep(delayInMs / 2); // delays in second
return c;
}