File: brutalchessconsole.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 (170 lines) | stat: -rw-r--r-- 4,577 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
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
// Brutal Chess
//
// File : brutalchessconsole.cpp
// Date : 05/03/2005
// Authors : Maxwell Lazaroff and Michael Cook
//
// Description : Driver file for the game.

#include<iostream>
#include "board.h"
#include "boardmove.h"
#include "chessplayer.h"
#include "alphabetaplayer.h"
#include "humanplayer.h"
using namespace std;

/***************************************************************************
*	Error Codes
***************************************************************************/

const int NOERR = 0;

void 
printBanner(){
	cout << endl;
	cout << "=======================================================\n";
	cout << "                Welcome To Brutal Chess                \n";
	cout << "=======================================================\n";

	return;
}

bool
askToQuit(){
	
	string userResponse;
	const string YES = "yes";
	const string NO  = "no";
	
	cout << "Would you like to quit? [yes/no]: ";
	cin >> userResponse;
	
	if (userResponse == YES){
		return true;
	} else if (userResponse == NO){
		return false;
	} else{
		cout << "Please type 'yes' or 'no'...\n";
		return askToQuit();
	}
}

void
usage(){

    cout << "==========================================================" <<endl;
    cout << "                   BRUTAL CHESS USAGE                     " <<endl;
    cout << "==========================================================" <<endl;
    cout << "                                                          " <<endl;
    cout << " ./brutalchessconsole <Player 1 (W)> <Player 2 (B)> <First Move>";
	cout << endl;
    cout << "                                                          " <<endl;
    cout << " <Player1 (W)> - Enter either your name or the type of    " <<endl;
    cout << "                 AI (AI TYPES HERE) you wish to play.     " <<endl;
    cout << " <Player2 (B)> - Use exactly as the <Player1 (W)> option. " <<endl;
    cout << " <FirstMove>   - Enter either 'W' or 'B', whoever you wish" <<endl;
    cout << "                 to have the first move                   " <<endl;
    cout << "                                                          " <<endl;
    cout << "==========================================================" <<endl;
}

    
	
	
/***************************************************************************
*	Main
***************************************************************************/

int 
main(int argc, char *argv[]){

    if(argc != 4)
    {
        usage();
        return 1;
    }

    ChessPlayer * _playOne = new HumanPlayer(argv[1]);
    ChessPlayer * _playTwo = new AlphaBetaPlayer;


    
    if(_playOne->getName() == _playTwo->getName())
    {
      _playOne->append_name("_1");
      _playTwo->append_name("_2");
    }

	printBanner();

	bool quitGame   = false;
	bool gameOver   = false;
	int  roundCount = 2;
	bool player     = true; // True == White's turn
	
	if(argv[3] == "B")
	   player = false;
	
	Board board;
	
	BoardMove currentmove;
	
	while (!quitGame){
		// Loops while the user has not quit Brutal Chess.
		cout << "Starting a new game.\n";
		
		while (!gameOver){
			board.print();
			cout << "Brutal Chess!" << endl << _playOne->getName();
			cout << "(white) VERSUS ";
			cout << _playTwo->getName() << "(black), on round ";
			cout << roundCount/2 <<endl;
			
			if(player)
            {
                cout << _playOne->getName() << "'s move..." << endl;
                currentmove = (*_playOne).decide_move(board, player);
            }
			if(!player)
			{
				cout << _playTwo->getName() << "'s move..." << endl;
				currentmove = (*_playTwo).decide_move(board, player);
			}
			
			cout << currentmove;
			board.updateBoard(currentmove);
			
			if(player)
			{
                if((*_playTwo).inCheckmate(board, player))
                {
                    cout << endl;
                    cout << _playTwo->getName() << " is in checkmate!" << endl;
                    cout << _playOne->getName() << " wins the game!" << endl;
                    gameOver = true;
                }
            }
            else
			{
                if((*_playOne).inCheckmate(board, player))
                {
                    cout << endl;
                    cout << _playOne->getName() << " is in checkmate!" << endl;
                    cout << _playTwo->getName() << " wins the game!" << endl;
                    gameOver = true;
                }
            }
						
			roundCount++;
			player = !player;
		}
		
		// Asks the user to quit, or to play another game.
		quitGame = askToQuit();
	}

	return NOERR;
}

// End of file brutalchessconsole.cpp