File: randomplayer.cpp

package info (click to toggle)
brutalchess 0.0.20060314cvs-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,496 kB
  • ctags: 447
  • sloc: cpp: 6,696; makefile: 118
file content (110 lines) | stat: -rw-r--r-- 2,209 bytes parent folder | download
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
/***************************************************************************
*	Brutal Chess Pre-Alpha Build
*	- randomplayer.cpp
*
*	Authors: Maxwell Lazaroff, Michael Cook, and Joe Flint
*	Date Created : May 3th, 2005
*	Last Modified: June 2nd, 2005
*
*	- description - Implements the methods of RandomPlayer.
***************************************************************************/

#include <time.h>

#include "randomplayer.h"

BoardMove
RandomPlayer::decide_move(const Board & board, bool & white)
{
	bool checkedstarts[8][8];
	bool checkedends[8][8];
	for(int i = 0; i < 8; i++)
	{
		for(int j = 0; j < 8; j++)
		{
			checkedstarts[i][j] = false;
		}
	}

	srand((unsigned)time(NULL));

	uint startrow = 0;
	uint startcol = 0;

	uint endrow;
	uint endcol;

	int endmovecount = 0;

	BoardMove move;

	bool goodmove = false;
	while(!goodmove)
	{
		startrow = rand()%8;
		startcol = rand()%8;
		while(checkedstarts[startrow][startcol]) {
			startrow = rand()%8;
			startcol = rand()%8;
		}

		for(int i = 0; i < 8; i++)
		{
			for(int j = 0; j < 8; j++)
			{
				checkedends[i][j] = false;
			}
		}

		endmovecount = 0;

		if(white && board._board[startrow][startcol].getColor() == WHITE)
		{
			while(endmovecount < 64 && !goodmove)
			{
				endrow = rand()%8;
				endcol = rand()%8;
				while(checkedends[endrow][endcol])
				{
					endrow = rand()%8;
					endcol = rand()%8;
				}

				move.setStartx(startrow);
				move.setStarty(startcol);
				move.setEndx(endrow);
				move.setEndy(endcol);

				goodmove = check_move(move, board, white);
				checkedends[endrow][endcol] = true;
				endmovecount++;
			}
		}
		if(!white && board._board[startrow][startcol].getColor() == BLACK)
		{
			while(endmovecount < 64 && !goodmove)
			{
				endrow = rand()%8;
				endcol = rand()%8;
				while(checkedends[endrow][endcol])
				{
					endrow = rand()%8;
					endcol = rand()%8;
				}

				move.setStartx(startrow);
				move.setStarty(startcol);
				move.setEndx(endrow);
				move.setEndy(endcol);

				goodmove = check_move(move, board, white);
				checkedends[endrow][endcol] = true;
				endmovecount++;
			}
		}
		checkedstarts[startrow][startcol] = true;
	}
	return move;
}

// End of file randomplayer.cpp