class move
This commit is contained in:
parent
1fcc420ae7
commit
24efbe7396
Binary file not shown.
Binary file not shown.
Binary file not shown.
81
board.py
81
board.py
@ -162,84 +162,3 @@ class Board:
|
||||
straightline_moves(False)
|
||||
elif piece.name == 'king':
|
||||
king_moves()
|
||||
def move_rival(self, from_row, from_column, to_row, to_column):
|
||||
#update
|
||||
self.x = self.board.boardlist[from_row][from_column].piece
|
||||
self.board.boardlist[from_row][from_column].piece = None
|
||||
if (self.board.boardlist[to_row][to_column].has_piece()):
|
||||
if (self.board.boardlist[to_row][to_column].piece.name == 'king'):
|
||||
self.board.boardlist[to_row][to_column].piece = self.x
|
||||
exit(0)
|
||||
self.board.boardlist[to_row][to_column].piece = self.x
|
||||
self.board.boardlist[to_row][to_column].piece.moved = True
|
||||
self.board.boardlist[to_row][to_column].piece.delete_moves()
|
||||
return self.board.boardlist[to_row][to_column].piece.color
|
||||
def move_boot(self, color):
|
||||
self.boot_piece = []
|
||||
self.piece_row_column = []
|
||||
for r in range(0, 8):
|
||||
for c in range(0, 8):
|
||||
if self.board.boardlist[r][c].piece != None:
|
||||
if self.board.boardlist[r][c].piece.color != color:
|
||||
self.boot_piece.append(self.board.boardlist[r][c])
|
||||
|
||||
self.value = random.randrange(0, len(self.boot_piece))
|
||||
self.initial = self.boot_piece[self.value]
|
||||
while len(self.initial.piece.valid_moves) == 0:
|
||||
self.value = random.randrange(0, len(self.boot_piece))
|
||||
self.initial = self.boot_piece[self.value]
|
||||
self.initial_row=self.initial.row
|
||||
self.initial_column = self.initial.column
|
||||
self.value = random.randrange(0, len(self.initial.piece.valid_moves))
|
||||
self.final = self.initial.piece.valid_moves[self.value]
|
||||
self.final_row = self.final[0]
|
||||
self.final_column = self.final[1]
|
||||
self.x = self.board.boardlist[self.initial_row][self.initial_column].piece
|
||||
self.board.boardlist[self.initial_row][self.initial_column].piece = None
|
||||
if (self.board.boardlist[self.final_row][self.final_column].has_piece()):
|
||||
if (self.board.boardlist[self.final_row][self.final_column].piece.name== 'king'):
|
||||
self.board.boardlist[self.final_row][self.final_column].piece = self.x
|
||||
exit(0)
|
||||
self.board.boardlist[self.final_row][self.final_column].piece = self.x
|
||||
#pawn promotion
|
||||
if isinstance(self.x, Pawn):
|
||||
if (self.final_row==0) or (self.final_row==7):
|
||||
self.board.boardlist[self.final_row][self.final_column].piece = Queen(self.x.color)
|
||||
self.board.boardlist[self.final_row][self.final_column].piece.moved = True
|
||||
self.board.boardlist[self.final_row][self.final_column].piece.delete_moves()
|
||||
|
||||
if self.initial_column == 0:
|
||||
self.initial_column = "A"
|
||||
elif self.initial_column == 1:
|
||||
self.initial_column = "B"
|
||||
elif self.initial_column == 2:
|
||||
self.initial_column = "C"
|
||||
elif self.initial_column == 3:
|
||||
self.initial_column = "D"
|
||||
elif self.initial_column == 4:
|
||||
self.initial_column = "E"
|
||||
elif self.initial_column == 5:
|
||||
self.initial_column = "F"
|
||||
elif self.initial_column == 6:
|
||||
self.initial_column = "G"
|
||||
elif self.initial_column == 7:
|
||||
self.initial_column = "H"
|
||||
|
||||
if self.final_column == 0:
|
||||
self.final_column = "A"
|
||||
elif self.final_column == 1:
|
||||
self.final_column = "B"
|
||||
elif self.final_column == 2:
|
||||
self.final_column = "C"
|
||||
elif self.final_column == 3:
|
||||
self.final_column = "D"
|
||||
elif self.final_column == 4:
|
||||
self.final_column = "E"
|
||||
elif self.final_column == 5:
|
||||
self.final_column = "F"
|
||||
elif self.final_column == 6:
|
||||
self.final_column = "G"
|
||||
elif self.final_column == 7:
|
||||
self.final_column = "H"
|
||||
self.ruch = str(self.initial_column) + str(self.initial_row) + ' ' + str(self.final_column) + str(self.final_row)
|
||||
return self.ruch
|
8
game.py
8
game.py
@ -1,4 +1,5 @@
|
||||
from board import Board
|
||||
from move import Move
|
||||
import random
|
||||
|
||||
class Game:
|
||||
@ -75,7 +76,7 @@ class Game:
|
||||
self.currently_available_moves()
|
||||
|
||||
if ruch == None:
|
||||
return Board.move_boot(self, 'black')
|
||||
return Move.move_boot(self, 'black')
|
||||
|
||||
else:
|
||||
self.from_column = ruch[0]
|
||||
@ -116,7 +117,6 @@ class Game:
|
||||
self.to_column = 6
|
||||
elif self.to_column == 'H':
|
||||
self.to_column = 7
|
||||
|
||||
self.color = Board.move_rival(self, self.from_row, self.from_column, self.to_row, self.to_column)
|
||||
return Board.move_boot(self, self.color)
|
||||
self.color = Move.move_rival(self, self.from_row, self.from_column, self.to_row, self.to_column)
|
||||
return Move.move_boot(self, self.color)
|
||||
|
||||
|
2
main.py
2
main.py
@ -5,7 +5,9 @@ game.printBoard()
|
||||
game2.printBoard()
|
||||
ruch=None
|
||||
while True:
|
||||
#print(ruch)
|
||||
ruch = game.nextMove(ruch)
|
||||
game.printBoard()
|
||||
#print(ruch)
|
||||
ruch = game2.nextMove(ruch)
|
||||
game2.printBoard()
|
||||
|
82
move.py
82
move.py
@ -0,0 +1,82 @@
|
||||
from board import Board
|
||||
from square import Square
|
||||
from pawn import Pawn
|
||||
from knight import Knight
|
||||
from bishop import Bishop
|
||||
from rook import Rook
|
||||
from queen import Queen
|
||||
from king import King
|
||||
import random
|
||||
|
||||
class Move:
|
||||
@staticmethod
|
||||
def move_rival(self, from_row, from_column, to_row, to_column):
|
||||
#update
|
||||
self.x = self.board.boardlist[from_row][from_column].piece
|
||||
self.board.boardlist[from_row][from_column].piece = None
|
||||
if (self.board.boardlist[to_row][to_column].has_piece()):
|
||||
if (self.board.boardlist[to_row][to_column].piece.name == 'king'):
|
||||
self.board.boardlist[to_row][to_column].piece = self.x
|
||||
exit(0)
|
||||
self.board.boardlist[to_row][to_column].piece = self.x
|
||||
self.board.boardlist[to_row][to_column].piece.moved = True
|
||||
self.board.boardlist[to_row][to_column].piece.delete_moves()
|
||||
return self.board.boardlist[to_row][to_column].piece.color
|
||||
def move_boot(self, color):
|
||||
self.boot_piece = []
|
||||
self.piece_row_column = []
|
||||
for r in range(0, 8):
|
||||
for c in range(0, 8):
|
||||
if self.board.boardlist[r][c].piece != None:
|
||||
if self.board.boardlist[r][c].piece.color != color:
|
||||
self.boot_piece.append(self.board.boardlist[r][c])
|
||||
|
||||
self.value = random.randrange(0, len(self.boot_piece))
|
||||
self.initial = self.boot_piece[self.value]
|
||||
while len(self.initial.piece.valid_moves) == 0:
|
||||
self.value = random.randrange(0, len(self.boot_piece))
|
||||
self.initial = self.boot_piece[self.value]
|
||||
self.initial_row=self.initial.row
|
||||
self.initial_column = self.initial.column
|
||||
self.value = random.randrange(0, len(self.initial.piece.valid_moves))
|
||||
self.final = self.initial.piece.valid_moves[self.value]
|
||||
self.final_row = self.final[0]
|
||||
self.final_column = self.final[1]
|
||||
self.x = self.board.boardlist[self.initial_row][self.initial_column].piece
|
||||
self.board.boardlist[self.initial_row][self.initial_column].piece = None
|
||||
if (self.board.boardlist[self.final_row][self.final_column].has_piece()):
|
||||
if (self.board.boardlist[self.final_row][self.final_column].piece.name== 'king'):
|
||||
self.board.boardlist[self.final_row][self.final_column].piece = self.x
|
||||
exit(0)
|
||||
self.board.boardlist[self.final_row][self.final_column].piece = self.x
|
||||
#pawn promotion
|
||||
if isinstance(self.x, Pawn):
|
||||
if (self.final_row==0) or (self.final_row==7):
|
||||
self.board.boardlist[self.final_row][self.final_column].piece = Queen(self.x.color)
|
||||
self.board.boardlist[self.final_row][self.final_column].piece.moved = True
|
||||
self.board.boardlist[self.final_row][self.final_column].piece.delete_moves()
|
||||
#zmiana ruchu na litery
|
||||
self.initial_column=Move.numbers_to_letters(self.initial_column)
|
||||
self.final_column=Move.numbers_to_letters(self.final_column)
|
||||
|
||||
self.ruch = str(self.initial_column) + str(self.initial_row) + ' ' + str(self.final_column) + str(self.final_row)
|
||||
return self.ruch
|
||||
|
||||
def numbers_to_letters(number):
|
||||
if number == 0:
|
||||
number = "A"
|
||||
elif number == 1:
|
||||
number = "B"
|
||||
elif number == 2:
|
||||
number = "C"
|
||||
elif number == 3:
|
||||
number = "D"
|
||||
elif number == 4:
|
||||
number = "E"
|
||||
elif number == 5:
|
||||
number = "F"
|
||||
elif number == 6:
|
||||
number = "G"
|
||||
elif number == 7:
|
||||
number = "H"
|
||||
return number
|
Loading…
Reference in New Issue
Block a user