tries this ai it explain computer code.
https://denigma.app/?fbclid=IwAR2J4tBQkKp9IS_JiWo4VTyj5gJMqOWbBKT6NQuLyGcm0s5zMcOthN8Bseghow did it do?
"""CONVENTIONS:
positions are done row-column from the bottom left and are both numbers. This corresponds to the alpha-number system in traditional chess while being computationally useful. they are specified as tuples
"""
import itertools
WHITE = "white"
BLACK = "black"
class Game:
#ive decided since the number of pieces is capped but the type of pieces is not (pawn transformations), I've already coded much of the modularity to support just using a dictionary of pieces
def __init__(self):
self.playersturn = BLACK
self.message = "this is where prompts will go"
self.gameboard = {}
self.placePieces()
print("chess program. enter moves in algebraic notation separated by space")
self.main()
def placePieces(self):
for i in range(0,8):
self.gameboard[(i,1)] = Pawn(WHITE,uniDict[WHITE][Pawn],1)
self.gameboard[(i,6)] = Pawn(BLACK,uniDict[BLACK][Pawn],-1)
placers = [Rook,Knight,Bishop,Queen,King,Bishop,Knight,Rook]
for i in range(0,8):
self.gameboard[(i,0)] = placers[i](WHITE,uniDict[WHITE][placers[i]])
self.gameboard[((7-i),7)] = placers[i](BLACK,uniDict[BLACK][placers[i]])
placers.reverse()
def main(self):
while True:
self.printBoard()
print(self.message)
self.message = ""
startpos,endpos = self.parseInput()
try:
target = self.gameboard[startpos]
except:
self.message = "could not find piece; index probably out of range"
target = None
if target:
print("found "+str(target))
if target.Color != self.playersturn:
self.message = "you aren't allowed to move that piece this turn"
continue
if target.isValid(startpos,endpos,target.Color,self.gameboard):
self.message = "that is a valid move"
self.gameboard[endpos] = self.gameboard[startpos]
del self.gameboard[startpos]
self.isCheck()
if self.playersturn == BLACK:
self.playersturn = WHITE
else : self.playersturn = BLACK
else :
self.message = "invalid move" + str(target.availableMoves(startpos[0],startpos[1],self.gameboard))
print(target.availableMoves(startpos[0],startpos[1],self.gameboard))
else : self.message = "there is no piece in that space"
def isCheck(self):
#ascertain where the kings are, check all pieces of opposing color against those kings, then if either get hit, check if its checkmate
king = King
kingDict = {}
pieceDict = {BLACK : [], WHITE : []}
for position,piece in self.gameboard.items():
if type(piece) == King:
kingDict[piece.Color] = position
print(piece)
pieceDict[piece.Color].append((piece,position))
#white
if self.canSeeKing(kingDict[WHITE],pieceDict[BLACK]):
self.message = "White player is in check"
if self.canSeeKing(kingDict[BLACK],pieceDict[WHITE]):
self.message = "Black player is in check"
def canSeeKing(self,kingpos,piecelist):
#checks if any pieces in piece list (which is an array of (piece,position) tuples) can see the king in kingpos
for piece,position in piecelist:
if piece.isValid(position,kingpos,piece.Color,self.gameboard):
return True
def parseInput(self):
try:
a,b = input().split()
a = ((ord(a[0])-97), int(a[1])-1)
b = (ord(b[0])-97, int(b[1])-1)
print(a,b)
return (a,b)
except:
print("error decoding input. please try again")
return((-1,-1),(-1,-1))
"""def validateInput(self, *kargs):
for arg in kargs:
if type(arg[0]) is not type(1) or type(arg[1]) is not type(1):
return False
return True"""
def printBoard(self):
print(" 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |")
for i in range(0,8):
print("-"*32)
print(chr(i+97),end="|")
for j in range(0,8):
item = self.gameboard.get((i,j)," ")
print(str(item)+' |', end = " ")
print()
print("-"*32)
"""game class. contains the following members and methods:
two arrays of pieces for each player
8x8 piece array with references to these pieces
a parse function, which turns the input from the user into a list of two tuples denoting start and end points
a checkmateExists function which checks if either players are in checkmate
a checkExists function which checks if either players are in check (woah, I just got that nonsequitur)
a main loop, which takes input, runs it through the parser, asks the piece if the move is valid, and moves the piece if it is. if the move conflicts with another piece, that piece is removed. ischeck(mate) is run, and if there is a checkmate, the game prints a message as to who wins
"""
class Piece:
def __init__(self,color,name):
self.name = name
self.position = None
self.Color = color
def isValid(self,startpos,endpos,Color,gameboard):
if endpos in self.availableMoves(startpos[0],startpos[1],gameboard, Color = Color):
return True
return False
def __repr__(self):
return self.name
def __str__(self):
return self.name
def availableMoves(self,x,y,gameboard):
print("ERROR: no movement for base class")
def AdNauseum(self,x,y,gameboard, Color, intervals):
"""repeats the given interval until another piece is run into.
if that piece is not of the same color, that square is added and
then the list is returned"""
answers = []
for xint,yint in intervals:
xtemp,ytemp = x+xint,y+yint
while self.isInBounds(xtemp,ytemp):
#print(str((xtemp,ytemp))+"is in bounds")
target = gameboard.get((xtemp,ytemp),None)
if target is None: answers.append((xtemp,ytemp))
elif target.Color != Color:
answers.append((xtemp,ytemp))
break
else:
break
xtemp,ytemp = xtemp + xint,ytemp + yint
return answers
def isInBounds(self,x,y):
"checks if a position is on the board"
if x >= 0 and x < 8 and y >= 0 and y < 8:
return True
return False
def noConflict(self,gameboard,initialColor,x,y):
"checks if a single position poses no conflict to the rules of chess"
if self.isInBounds(x,y) and (((x,y) not in gameboard) or gameboard[(x,y)].Color != initialColor) : return True
return False
chessCardinals = [(1,0),(0,1),(-1,0),(0,-1)]
chessDiagonals = [(1,1),(-1,1),(1,-1),(-1,-1)]
def knightList(x,y,int1,int2):
"""sepcifically for the rook, permutes the values needed around a position for noConflict tests"""
return [(x+int1,y+int2),(x-int1,y+int2),(x+int1,y-int2),(x-int1,y-int2),(x+int2,y+int1),(x-int2,y+int1),(x+int2,y-int1),(x-int2,y-int1)]
def kingList(x,y):
return [(x+1,y),(x+1,y+1),(x+1,y-1),(x,y+1),(x,y-1),(x-1,y),(x-1,y+1),(x-1,y-1)]
class Knight(Piece):
def availableMoves(self,x,y,gameboard, Color = None):
if Color is None : Color = self.Color
return [(xx,yy) for xx,yy in knightList(x,y,2,1) if self.noConflict(gameboard, Color, xx, yy)]
class Rook(Piece):
def availableMoves(self,x,y,gameboard ,Color = None):
if Color is None : Color = self.Color
return self.AdNauseum(x, y, gameboard, Color, chessCardinals)
class Bishop(Piece):
def availableMoves(self,x,y,gameboard, Color = None):
if Color is None : Color = self.Color
return self.AdNauseum(x, y, gameboard, Color, chessDiagonals)
class Queen(Piece):
def availableMoves(self,x,y,gameboard, Color = None):
if Color is None : Color = self.Color
return self.AdNauseum(x, y, gameboard, Color, chessCardinals+chessDiagonals)
class King(Piece):
def availableMoves(self,x,y,gameboard, Color = None):
if Color is None : Color = self.Color
return [(xx,yy) for xx,yy in kingList(x,y) if self.noConflict(gameboard, Color, xx, yy)]
class Pawn(Piece):
def __init__(self,color,name,direction):
self.name = name
self.Color = color
#of course, the smallest piece is the hardest to code. direction should be either 1 or -1, should be -1 if the pawn is traveling "backwards"
self.direction = direction
def availableMoves(self,x,y,gameboard, Color = None):
if Color is None : Color = self.Color
answers = []
if (x+1,y+self.direction) in gameboard and self.noConflict(gameboard, Color, x+1, y+self.direction) : answers.append((x+1,y+self.direction))
if (x-1,y+self.direction) in gameboard and self.noConflict(gameboard, Color, x-1, y+self.direction) : answers.append((x-1,y+self.direction))
if (x,y+self.direction) not in gameboard and Color == self.Color : answers.append((x,y+self.direction))# the condition after the and is to make sure the non-capturing movement (the only fucking one in the game) is not used in the calculation of checkmate
return answers
uniDict = {WHITE : {Pawn : "♙", Rook : "♖", Knight : "♘", Bishop : "♗", King : "♔", Queen : "♕" }, BLACK : {Pawn : "♟", Rook : "♜", Knight : "♞", Bishop : "♝", King : "♚", Queen : "♛" }}
Game()
--------------------------------------
- The code is a class that is called Game.
- It has two methods, __init__ and main.
- The __init__ method creates the board of pieces for the game, while the main method does all of the work in this program.
- The first thing to note about this code is that it uses tuples instead of lists or dictionaries because they are more efficient computationally speaking.
- This means that each piece on the board will be stored as a tuple with three elements: an index number (0-8), a color (white or black), and an instance of Piece(WHITE).
- In order to place pieces on the board, you have to iterate through every possible position from 0-8 until you find one where your target piece can go.
- You do this by using range() function which takes two arguments: startpos and endpos which correspond to row and column numbers respectively.
- For example if I wanted my pawns at positions 1-4, I would use range(1,4) which corresponds to rows 1-3 since there are 8 columns in total so 4 is not included in those ranges but 5 is included since it's between 2 and 3 so it's considered part of both ranges even though its not actually- The code is a snippet of the beginning of the main function.
- It starts by printing "chess program."
- and then prompts for moves in algebraic notation separated by space.
- The placePieces function iterates through each piece type (Pawn, Rook, Knight, Bishop) and creates an instance of that piece class with a dictionary keyed to its name as well as 1 or -1 as its starting position on the board.
- The pieces are all placed on the gameboard which is initialized with 8 rows and columns (0-7).
- The placers list contains all possible positions for each piece type and their corresponding values in terms of how many times they can be moved from that position before being blocked off by another piece or reaching it- The code starts by declaring a few variables.
- The first is self, which is the object that will be used to represent the player in this game.
- Next, there are two integers: startpos[0],startpos[1].
- These are coordinates on the board where the player starts their turn.
- The next line declares an input variable called a and b.
- These variables contain whatever was typed into the program when it started up (in other words, they're initialized with what's been typed so far).
- They're split at whitespace because Python can't handle strings of more than one character without splitting them up into individual characters.
- This means that you have to type "a" or "b" instead of just "a".
- Next comes parseInput(), which takes any string as input and parses it for valid pieces of information before returning either (-1,-1) or (a,b).
- If no piece is found in either list then -1,-1 would be returned; if a piece is found but not both lists then only one value would be returned from parseInput().- The code is used to print the board in an 8x8 grid.
- The code starts by printing out a "1" and then prints out each of the pieces on the board, with their respective colors:
- BLACK: 1 | 2 | 3 | 4 | 5 | 6 | 7 WHITE: 8- The code starts with a class definition for the game.
- It contains two arrays of pieces, one for each player.
- The 8x8 piece array has references to these pieces and a parse function which turns the input from the user into a list of two tuples denoting start and end points.
- There is also a checkmateExists function which checks if either players are in checkmate, and there is also a checkExists function which checks if either players are in check (woah, I just got that nonsequitur).
- The main loop takes input, runs it through the parser, asks the piece if the move is valid, and moves it if it is.
- If there's an error or conflict with another piece on board then that piece gets removed from board.
- IsCheck(mate) will be run at some point during this process as well as when someone wins by checking their opponent's king against their own king; otherwise "AdNauseum" will repeat until no other pieces remain on board or all possible moves have been made before returning an answer list containing all possible positions where neither player can move any further without running into another piece of different color than theirs.- The code is a Python class definition for the game of Checkers.
- The class contains two arrays of pieces, 8x8 piece array with references to these pieces, a parse function which turns the input from the user into a list of two tuples denoting start and end points, a checkmateExists function which checks if either players are in checkmate, and finally an ischeck(mate) function which checks if there is a checkmate.
- The main loop takes input from the user through their keyboard or mouse and runs it through the parser before asking each piece if its move is valid.
- If any move conflicts with another piece on board then that other piece will be removed.
- If both players are in check (woah I just got- The code is a chess game that uses the rules of chess.
- The code is written in Python and has classes for pieces, which are called "Piece".
- The first class is Knight, which represents a piece on the board.
- It has methods for checking if there's no conflict with other pieces on the board.
- There are also methods to check if it can move or not depending on its position and color.
- The second class is Rook, which represents a piece that moves like a knight but can only go two spaces at once instead of three because it doesn't have an availableMove method.
- It also has an AdNauseum method to check if there's any conflicts with other pieces on the board before moving itself around.
- The third class is Bishop, which represents a piece that moves like a rook but can only go one space at once because it doesn't have an availableMove method either.
- It also has an AdNauseum method to check if there's any conflicts with other pieces on the board before moving itself around too.- The code is a simple example of how to use the noConflict() function.
- The following code shows an example of how to use the availableMoves() function for a piece.
–