-
Notifications
You must be signed in to change notification settings - Fork 1
/
PythonChessMain.py
executable file
·149 lines (123 loc) · 5.26 KB
/
PythonChessMain.py
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
#! /usr/bin/env python
"""
Project: Python Chess
File name: PythonChessMain.py
Description: Chess for player vs. player, player vs. AI, or AI vs. AI.
Uses Tkinter to get initial game parameters.
Uses Pygame to draw the board and pieces and to get user mouse clicks.
Copyright (C) 2009 Steve Osborne, srosborne (at) gmail.com
*******
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******
Version history:
v 0.4 - 14 April 2009. Added better chess piece graphics from Wikimedia
Commons. Added a Tkinter dialog box (ChessGameParams.py) for getting
the game setup parameters. Converted to standard chess notation for
move reporting and added row/col labels around the board.
v 0.3 - 06 April 2009. Added pygame graphical interface. Includes
addition of ScrollingTextBox class.
v 0.2 - 04 April 2009. Broke up the program into classes that will
hopefully facilitate easily incorporating graphics or AI play.
v 0.1 - 01 April 2009. Initial release. Draws the board, accepts
move commands from each player, checks for legal piece movement.
Appropriately declares player in check or checkmate.
To do:
Add in better AI
"""
from ChessBoard import ChessBoard
from ChessAI import ChessAI
from ChessPlayer import ChessPlayer
from ChessGUI_text import ChessGUI_text
from ChessGUI_pygame import ChessGUI_pygame
from ChessRules import ChessRules
from ChessGameParams import TkinterGameSetupParams
import sys
class PythonChessMain:
def __init__(self,options):
if 'd' in options:
self.Board = ChessBoard(2)
self.debugMode = True
else:
self.Board = ChessBoard(0)#0 for normal board setup; see ChessBoard class for other options (for testing purposes)
self.debugMode = False
self.Rules = ChessRules()
def SetUp(self,options):
#gameSetupParams: Player 1 and 2 Name, Color, Human/AI level
if self.debugMode:
player1Name = 'Steve'
player1Type = 'human'
player1Color = 'white'
player2Name = 'Bob'
player2Type = 'AI'
player2Color = 'black'
else:
GameParams = TkinterGameSetupParams()
(player1Name, player1Color, player1Type, player2Name, player2Color, player2Type) = GameParams.GetGameSetupParams()
self.player = [0,0]
if player1Type == 'human':
self.player[0] = ChessPlayer(player1Name,player1Color)
else:
self.player[0] = ChessAI(player1Name,player1Color)
if player2Type == 'human':
self.player[1] = ChessPlayer(player2Name,player2Color)
else:
self.player[1] = ChessAI(player2Name,player2Color)
#create the gui object - didn't do earlier because pygame conflicts with any gui manager (Tkinter, WxPython...)
if 't' in options:
self.guitype = 'text'
self.Gui = ChessGUI_text()
else:
self.guitype = 'pygame'
if 'o' in options:
self.Gui = ChessGUI_pygame(0)
else:
self.Gui = ChessGUI_pygame(1)
def MainLoop(self):
currentPlayerIndex = 0
turnCount = 0
while not self.Rules.IsCheckmate(self.Board.GetState(),self.player[currentPlayerIndex].color):
board = self.Board.GetState()
currentColor = self.player[currentPlayerIndex].GetColor()
#hardcoded so that player 1 is always white
if currentColor == 'white':
turnCount = turnCount + 1
self.Gui.Draw(board)
self.Gui.PrintMessage("")
self.Gui.PrintMessage("-----------TURN "+str(turnCount)+" - "+ self.player[currentPlayerIndex].GetName() +" ("+currentColor+")-----------")
if self.Rules.IsInCheck(board,currentColor):
self.Gui.PrintMessage("Warning..."+self.player[currentPlayerIndex].GetName()+" ("+self.player[currentPlayerIndex].GetColor()+") is in check!")
if self.player[currentPlayerIndex].GetType() == 'AI':
moveTuple = self.player[currentPlayerIndex].GetMove(self.Board.GetState(), currentColor)
else:
moveTuple = self.Gui.GetPlayerInput(board,currentColor)
moveReport = self.Board.MovePiece(moveTuple) #moveReport = string like "White Bishop moves from A1 to C3" (+) "and captures ___!"
self.Gui.PrintMessage(moveReport)
currentPlayerIndex = (currentPlayerIndex+1)%2 #this will cause the currentPlayerIndex to toggle between 1 and 0
self.Gui.PrintMessage("CHECKMATE!")
winnerIndex = (currentPlayerIndex+1)%2
self.Gui.PrintMessage(self.player[winnerIndex].GetName()+" ("+self.player[winnerIndex].GetColor()+") won the game!")
self.Gui.Draw(board) #draw board a final time to show the end game
c = raw_input("Game finished...press any key to quit.")#pause at the end
if self.guitype == 'pygame':
pygame.quit()
options = ""
if 'debug' in sys.argv:
options = options + 'd'
#text gui:
if 'text' in sys.argv:
options = options + 't'
#old graphics:
if 'old' in sys.argv:
options = options + 'o'
game = PythonChessMain(options)#'d' for debug, 't' for text-graphics
game.SetUp(options)
game.MainLoop()