**Take a look at this impressive piece of programming by Sam in Year 11. It really gives you an insight into the complexities of computer science. **

“Terrible chess” is a program I wrote that created almost the worst chess AI that could ever exist. This isn’t hyperbole – the AI makes all decisions at random and has no concept of preservation of the king. The only AI that could be worse than it would lose deliberately.

So why would I spend hours of my time creating this AI when I could be doing better things with my time?

Simply to make it play against itself.

These two AI would have a perfectly equal skill level. Admittedly, the skill level was 0, but it still allowed me to do my experiment.

What was my experiment? I wanted to find out if the old saying was correct; “white begins and black wins.” I wanted to find out which colour gave you an edge. Along the way, I also found out an average game length, along with some strange glitches, some of which I still have not removed because I have no idea what causes them.

I coded in python, for no reason other than that it is the only programming language I know in depth. The program is long, 782 lines long, the longest program I have ever created.

5000 games were played. While this is not a very large sample space, I feel this is large enough. 250,449 turns, where one turn was a white move, then a black move, occurred, an average of just over 50 per game. This average possibly should be higher, but most long games crashed the program.

The longest game was 200 moves, though, again, it could be higher.

The shortest was 1.

I have tried to explain how this could be possible, but I am forced to admit it must be one of the strange glitches I mentioned earlier. Even using my arbitrary “turns” that would allow three moves to have happened there is no way for a king to have been taken that early. Therefore, I believe that this occurred due to chance, and the subprogram for finding stalemates did not find a movable piece, just due to random chance.

Finally, I reach the results of the games. These were the original aim of this program, the only reason I spent hours of my lunch breaks constantly coding, testing, being confused at why the program crashed when I tried to get it to do 20 games at a time, and fixing.

Before these results, however, I must mention that this is not an accurate test. This program has many faults, such as pawns not being able to take pieces while on the edge, or there being no form of check and just relying on the king being taken. However, I believe that this is still accurate enough to discover which colour is best.

So, the results; in 5000 games there were…

111 draws…

2330 white wins and…

2559 black wins.

In percentages, that’s 2.22% draw, 46.6% white and 51.18% black. Black wins almost 10% more games than white does.

So, if you want an edge on someone while playing chess, play black. If you have an even skill level, chances are you will win 10% more than them.

**Here is the fantastic coding!**

from random import * def boardreset(): board = [["bR","bK","bB","bQ","BK","bB","bK","bR"], ["bP","bP","bP","bP","bP","bP","bP","bP"], ["__","__","__","__","__","__","__","__"], ["__","__","__","__","__","__","__","__"], ["__","__","__","__","__","__","__","__"], ["__","__","__","__","__","__","__","__"], ["wP","wP","wP","wP","wP","wP","wP","wP"], ["wR","wK","wB","wQ","WK","wB","wK","wR"]] drawmoves = 0 totalmoves = 0 board = [["bR","bK","bB","bQ","BK","bB","bK","bR"], ["bP","bP","bP","bP","bP","bP","bP","bP"], ["__","__","__","__","__","__","__","__"], ["__","__","__","__","__","__","__","__"], ["__","__","__","__","__","__","__","__"], ["__","__","__","__","__","__","__","__"], ["wP","wP","wP","wP","wP","wP","wP","wP"], ["wR","wK","wB","wQ","WK","wB","wK","wR"]] def printboard(): for i in range(0,8): print(board[i]) def move(x1,y1,x2,y2): #note - coordinate system is reversed (y,x) and origin is top left. no clear way to change. piece = board[y1][x1] if board[y2][x2] != "__": global drawmoves drawmoves = 0 board[y1][x1] = "__" board[y2][x2] = piece def pawn(x,y,colour,testing): if colour == "black": if x - 1 > -1 and x + 1 < 8 and y + 1 < 8:#change so piece on edge can take piece if board[y + 1][x - 1][0] == "w" or board[y + 1][x - 1][0] == "W" or board[y + 1][x + 1][0] == "w" or board[y + 1][x + 1][0] == "W": if testing == True: return True choosing = True while choosing == True: random = randint(0,3) if random == 3 and (board[y + 1][x - 1][0] == "w" or board[y + 1][x - 1][0] == "W"): move(x, y, x - 1, y + 1) choosing = False elif random == 2 and (board[y + 1][x + 1][0] == "w" or board[y + 1][x + 1][0] == "W"): move(x, y, x + 1, y + 1) choosing = False elif random == 1 and board[y + 1][x] == "__": move(x, y, x, y + 1) choosing = False elif random == 0 and y == 1 and board[y + 2][x] == "__" and board[y + 1][x] == "__": move(x, y, x, y + 2) choosing = False else: choosing = True elif y + 1 < 8: if board[y + 1][x] == "__": if testing == True: return True elif y == 1 and board[y + 2][x] == "__": random = randint(0,1) if random == 1: move(x, y, x, y + 2) else: move(x, y, x, y + 1) else: move(x, y, x, y + 1) if y == 7: pieces = ["bR", "bB", "bK", "bQ"] board[y][x] = pieces[randint(0,3)] else: if x - 1 > -1 and x + 1 < 8 and y - 1 > -1: if board[y - 1][x - 1][0] == "b" or board[y - 1][x - 1][0] == "B" or board[y - 1][x + 1][0] == "b" or board[y - 1][x + 1][0] == "B": if testing == True: return True choosing = True while choosing == True: random = randint(0,3) if random == 2 and (board[y - 1][x - 1][0] == "b" or board[y - 1][x - 1][0] == "B"): move(x, y, x - 1, y - 1) choosing = False elif random == 1 and (board[y - 1][x + 1][0] == "b" or board[y - 1][x + 1][0] == "B"): move(x, y, x + 1, y - 1) choosing = False elif random == 0 and board[y - 1][x] == "__": move(x, y, x, y - 1) choosing = False elif random == 0 and y == 6 and board[y - 2][x] == "__" and board[y - 1][x] == "__": move(x, y, x, y - 2) choosing = False else: choosing = True elif y - 1 > -1: if board[y - 1][x] == "__": if testing == True: return True elif y == 6 and board[y - 2][x] == "__": random = randint(0,1) if random == 1: move(x, y, x, y - 2) else: move(x, y, x, y - 1) else: move(x, y, x, y - 1) if y == 0: pieces = ["wR", "wB", "wK", "wQ"] board[y][x] = pieces[randint(0,3)] if testing == True: return False global drawmoves drawmoves = 0 def knight(x,y,colour,testing): choosing = True timestried = 0 while choosing == True: random = randint(0,7) if random == 0: if (y-2 > -1 and x - 1 > -1): if(colour == "black" and board[y-2][x-1][0] != "b" and board[y-2][x-1][0] != "B") or (colour == "white" and board[y-2][x-1][0] != "w" and board[y-2][x-1][0] != "W"): if testing == True: return True else: move(x,y,x-1,y-2) choosing = False else: timestried += 1 else: timestried += 1 elif random == 1: if (y-2 > -1 and x + 1 < 8): if(colour == "black" and board[y-2][x+1][0] != "b" and board[y-2][x+1][0] != "B") or (colour == "white" and board[y-2][x+1][0] != "w" and board[y-2][x+1][0] != "W"): if testing == True: return True else: move(x,y,x+1,y-2) choosing = False else: timestried += 1 else: timestried += 1 elif random == 2: if (y-1 > -1 and x + 2 < 8): if(colour == "black" and board[y-1][x+2][0] != "b" and board[y-1][x+2][0] != "B") or (colour == "white" and board[y-1][x+2][0] != "w" and board[y-1][x+2][0] != "W"): if testing == True: return True else: move(x,y,x+2,y-1) choosing = False else: timestried += 1 else: timestried += 1 elif random == 3: if (y+1 < 8 and x + 2 < 8): if(colour == "black" and board[y+1][x+2][0] != "b" and board[y+1][x+2][0] != "B") or (colour == "white" and board[y+1][x+2][0] != "w" and board[y+1][x+2][0] != "W"): if testing == True: return True else: move(x,y,x+2,y+1) choosing = False else: timestried += 1 else: timestried += 1 elif random == 4: if (y+2 < 8 and x + 1 < 8): if(colour == "black" and board[y+2][x+1][0] != "b" and board[y+2][x+1][0] != "B") or (colour == "white" and board[y+2][x+1][0] != "w" and board[y+2][x+1][0] != "W"): if testing == True: return True else: move(x,y,x+1,y+2) choosing = False else: timestried += 1 else: timestried += 1 elif random == 5: if (y+2 < 8 and x - 1 > -1): if(colour == "black" and board[y+2][x-1][0] != "b" and board[y+2][x-1][0] != "B") or (colour == "white" and board[y+2][x-1][0] != "w" and board[y+2][x-1][0] != "W"): if testing == True: return True else: move(x,y,x-1,y+2) choosing = False else: timestried += 1 else: timestried += 1 elif random == 6: if (y+1 < 8 and x - 2 > -1): if(colour == "black" and board[y+1][x-2][0] != "b" and board[y+1][x-2][0] != "B") or (colour == "white" and board[y+1][x-2][0] != "w" and board[y+1][x-2][0] != "W"): if testing == True: return True else: move(x,y,x-2,y+1) choosing = False else: timestried += 1 else: timestried += 1 elif random == 7: if (y-1 > -1 and x - 2 > -1): if(colour == "black" and board[y-1][x-2][0] != "b" and board[y-1][x-2][0] != "B") or (colour == "white" and board[y-1][x-2][0] != "w" and board[y-1][x-2][0] != "W"): if testing == True: return True else: move(x,y,x-2,y-1) choosing = False else: timestried += 1 else: timestried += 1 if timestried >= 20 and testing == True: return False def bishop(x,y,colour,testing): choosing = True timestried = 0 while choosing == True: random = randint(0,3) if random == 0 and (y - 1 > -1 and x + 1 < 8): if y < 7 - x: smallestcoord = y else: smallestcoord = 7 - x random = randint(1, smallestcoord) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y-i][x+i][0] != "b" and board[y-i][x+i][0] != "B") or (colour == "white" and board[y-i][x+i][0] != "w" and board[y-i][x+i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y-i][x+i][0] == "w" or board[y-i][x+i][0] == "W")) or (colour == "white" and (board[y-i][x+i][0] == "b" or board[y-i][x+i][0] == "B")): move(x+(i-1),y-(i-1),x+i,y-i) moving = False else: move(x+(i-1),y-(i-1),x+i,y-i) choosing = False else: moving = False if i == 1: timestried += 1 if random == 1 and (y + 1 < 8 and x + 1 < 8): if y > x: smallestcoord = 7 - y else: smallestcoord = 7 - x random = randint(1, smallestcoord) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y+i][x+i][0] != "b" and board[y+i][x+i][0] != "B") or (colour == "white" and board[y+i][x+i][0] != "w" and board[y+i][x+i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y+i][x+i][0] == "w" or board[y+i][x+i][0] == "W")) or (colour == "white" and (board[y+i][x+i][0] == "b" or board[y+i][x+i][0] == "B")): move(x+(i-1),y+(i-1),x+i,y+i) moving = False else: move(x+(i-1),y+(i-1),x+i,y+i) choosing = False else: moving = False if i == 1: timestried += 1 if random == 2 and (x - 1 > -1 and y + 1 < 8): if x < 7 - y: smallestcoord = x else: smallestcoord = 7 - y random = randint(1, smallestcoord) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y+i][x-i][0] != "b" and board[y+i][x-i][0] != "B") or (colour == "white" and board[y+i][x-i][0] != "w" and board[y+i][x-i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y+i][x-i][0] == "w" or board[y+i][x-i][0] == "W")) or (colour == "white" and (board[y+i][x-i][0] == "b" or board[y+i][x-i][0] == "B")): move(x-(i-1),y+(i-1),x-i,y+i) moving = False else: move(x-(i-1),y+(i-1),x-i,y+i) choosing = False else: moving = False if i == 1: timestried += 1 elif random == 3 and (y - 1 > -1 and x - 1 > -1): if y < x: smallestcoord = y else: smallestcoord = x random = randint(1, smallestcoord) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y-i][x-i][0] != "b" and board[y-i][x-i][0] != "B") or (colour == "white" and board[y-i][x-i][0] != "w" and board[y-i][x-i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y-i][x-i][0] == "w" or board[y-i][x-i][0] == "W")) or (colour == "white" and (board[y-i][x-i][0] == "b" and board[y-i][x-i][0] == "B")): move(x-(i-1),y-(i-1),x-i,y-i) moving = False else: move(x-(i-1),y-(i-1),x-i,y-i) choosing = False else: moving = False if i == 1: timestried += 1 else: timestried += 1 if timestried >= 10 and testing == True: return False def rook(x,y,colour,testing): choosing = True timestried = 0 while choosing == True: random = randint(0,3) if random == 0 and (y - 1 > -1): random = randint(1, y) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y-i][x][0] != "b" and board[y-i][x][0] != "B") or (colour == "white" and board[y-i][x][0] != "w" and board[y-i][x][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y-i][x][0] == "w" or board[y-i][x][0] == "W")) or (colour == "white" and (board[y-i][x][0] == "b" or board[y-i][x][0] == "B")): move(x,y-(i-1),x,y-i) moving = False else: move(x,y-(i-1),x,y-i) choosing = False else: moving = False if i == 1: timestried += 1 if random == 1 and (x + 1 < 8): random = randint(1, 7 - x) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y][x+i][0] != "b" and board[y][x+i][0] != "B") or (colour == "white" and board[y][x+i][0] != "w" and board[y][x+i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y][x+i][0] == "w" or board[y][x+i][0] == "W")) or (colour == "white" and (board[y][x+i][0] == "b" or board[y][x+i][0] == "B")): move(x+(i-1),y,x+i,y) moving = False else: move(x+(i-1),y,x+i,y) choosing = False else: moving = False if i == 1: timestried += 1 if random == 2 and (y + 1 < 8): random = randint(1, 7 - y) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y+i][x][0] != "b" and board[y+i][x][0] != "B") or (colour == "white" and board[y+i][x][0] != "w" and board[y+i][x][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y+i][x][0] == "w" or board[y+i][x][0] == "W")) or (colour == "white" and (board[y+i][x][0] == "b" or board[y+i][x][0] == "B")): move(x,y+(i-1),x,y+i) moving = False else: move(x,y+(i-1),x,y+i) choosing = False else: moving = False if i == 1: timestried += 1 elif random == 3 and (x - 1 > -1): random = randint(1, x) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y][x-i][0] != "b" and board[y][x-i][0] != "B") or (colour == "white" and board[y][x-i][0] != "w" and board[y][x-i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y][x-i][0] == "w" or board[y][x-i][0] == "W")) or (colour == "white" and (board[y][x-i][0] == "b" or board[y][x-i][0] == "B")): move(x-(i-1),y,x-i,y) moving = False else: move(x-(i-1),y,x-i,y) choosing = False else: moving = False if i == 1: timestried += 1 else: timestried += 1 if timestried >= 10 and testing == True: return False def king(x,y,colour,testing): choosing = True timestried = 0 while choosing == True: random = randint(0,7) if random == 0 and (y - 1 > -1): if ((colour == "black" and board[y-1][x][0] != "b" and board[y-1][x][0] != "B") or (colour == "white" and board[y-1][x][0] != "w" and board[y-1][x][0] != "W")): if testing == True: return True else: move(x,y,x,y-1) choosing = False else: timestried += 1 if random == 1 and (x + 1 < 8): if ((colour == "black" and board[y][x+1][0] != "b" and board[y][x+1][0] != "B") or (colour == "white" and board[y][x+1][0] != "w" and board[y][x+1][0] != "W")): if testing == True: return True else: move(x,y,x+1,y) choosing = False else: timestried += 1 if random == 2 and (y + 1 < 8): if ((colour == "black" and board[y+1][x][0] != "b" and board[y+1][x][0] != "B") or (colour == "white" and board[y+1][x][0] != "w" and board[y+1][x][0] != "W")): if testing == True: return True else: move(x,y,x,y+1) choosing = False else: timestried += 1 elif random == 3 and (x - 1 > -1): if ((colour == "black" and board[y][x-1][0] != "b" and board[y][x-1][0] != "B") or (colour == "white" and board[y][x-1][0] != "w" and board[y][x-1][0] != "W")): if testing == True: return True else: move(x,y,x-1,y) choosing = False else: timestried += 1 if random == 4 and (y - 1 > -1 and x + 1 < 8): if ((colour == "black" and board[y-1][x+1][0] != "b" and board[y-1][x+1][0] != "B") or (colour == "white" and board[y-1][x+1][0] != "w" and board[y-1][x+1][0] != "W")): if testing == True: return True else: move(x,y,x+1,y-1) choosing = False else: timestried += 1 if random == 5 and (y + 1 < 8 and x + 1 < 8): if ((colour == "black" and board[y+1][x+1][0] != "b" and board[y+1][x+1][0] != "B") or (colour == "white" and board[y+1][x+1][0] != "w" and board[y+1][x+1][0] != "W")): if testing == True: return True else: move(x,y,x+1,y+1) choosing = False else: timestried += 1 if random == 6 and (x - 1 > -1 and y + 1 < 8): if ((colour == "black" and board[y+1][x-1][0] != "b" and board[y+1][x-1][0] != "B") or (colour == "white" and board[y+1][x-1][0] != "w" and board[y+1][x-1][0] != "W")): if testing == True: return True else: move(x,y,x-1,y+1) choosing = False else: timestried += 1 elif random == 7 and (y - 1 > -1 and x - 1 > -1): if (colour == "black" and board[y-1][x-1][0] != "b" and board[y-1][x-1][0] != "B") or (colour == "white" and board[y-1][x-1][0] != "w" and board[y-1][x-1][0] != "W"): if testing == True: return True else: move(x,y,x-1,y-1) choosing = False else: timestried += 1 else: timestried += 1 if timestried >= 20 and testing == True: return False def queen(x,y,colour,testing): choosing = True timestried = 0 while choosing == True: random = randint(0,7) if random == 0 and (y - 1 > -1): random = randint(1, y) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y-i][x][0] != "b" and board[y-i][x][0] != "B") or (colour == "white" and board[y-i][x][0] != "w" and board[y-i][x][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y-i][x][0] == "w" or board[y-i][x][0] == "W")) or (colour == "white" and (board[y-i][x][0] == "b" or board[y-i][x][0] == "B")): move(x,y-(i-1),x,y-i) moving = False else: move(x,y-(i-1),x,y-i) choosing = False else: moving = False if i == 1: timestried += 1 if random == 1 and (x + 1 < 8): random = randint(1, 7 - x) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y][x+i][0] != "b" and board[y][x+i][0] != "B") or (colour == "white" and board[y][x+i][0] != "w" and board[y][x+i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y][x+i][0] == "w" or board[y][x+i][0] == "W")) or (colour == "white" and (board[y][x+i][0] == "b" or board[y][x+i][0] == "B")): move(x+(i-1),y,x+i,y) moving = False else: move(x+(i-1),y,x+i,y) choosing = False else: moving = False if i == 1: timestried += 1 if random == 2 and (y + 1 < 8): random = randint(1, 7 - y) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y+i][x][0] != "b" and board[y+i][x][0] != "B") or (colour == "white" and board[y+i][x][0] != "w" and board[y+i][x][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y+i][x][0] == "w" or board[y+i][x][0] == "W")) or (colour == "white" and (board[y+i][x][0] == "b" or board[y+i][x][0] == "B")): move(x,y+(i-1),x,y+i) moving = False else: move(x,y+(i-1),x,y+i) choosing = False else: moving = False if i == 1: timestried += 1 elif random == 3 and (x - 1 > -1): random = randint(1, x) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y][x-i][0] != "b" and board[y][x-i][0] != "B") or (colour == "white" and board[y][x-i][0] != "w" and board[y][x-i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y][x-i][0] == "w" or board[y][x-i][0] == "W")) or (colour == "white" and (board[y][x-i][0] == "b" or board[y][x-i][0] == "B")): move(x-(i-1),y,x-i,y) moving = False else: move(x-(i-1),y,x-i,y) choosing = False else: moving = False if i == 1: timestried += 1 if random == 4 and (y - 1 > -1 and x + 1 < 8): if y < 7 - x: smallestcoord = y else: smallestcoord = 7 - x random = randint(1, smallestcoord) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y-i][x+i][0] != "b" and board[y-i][x+i][0] != "B") or (colour == "white" and board[y-i][x+i][0] != "w" and board[y-i][x+i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y-i][x+i][0] == "w" or board[y-i][x+i][0] == "W")) or (colour == "white" and (board[y-i][x+i][0] == "b" or board[y-i][x+i][0] == "B")): move(x+(i-1),y-(i-1),x+i,y-i) moving = False else: move(x+(i-1),y-(i-1),x+i,y-i) choosing = False else: moving = False if i == 1: timestried += 1 if random == 5 and (y + 1 < 8 and x + 1 < 8): if y > x: smallestcoord = 7 - y else: smallestcoord = 7 - x random = randint(1, smallestcoord) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y+i][x+i][0] != "b" and board[y+i][x+i][0] != "B") or (colour == "white" and board[y+i][x+i][0] != "w" and board[y+i][x+i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y+i][x+i][0] == "w" or board[y+i][x+i][0] == "W")) or (colour == "white" and (board[y+i][x+i][0] == "b" or board[y+i][x+i][0] == "B")): move(x+(i-1),y+(i-1),x+i,y+i) moving = False else: move(x+(i-1),y+(i-1),x+i,y+i) choosing = False else: moving = False if i == 1: timestried += 1 if random == 6 and (x - 1 > -1 and y + 1 < 8): if x < 7 - y: smallestcoord = x else: smallestcoord = 7 - y random = randint(1, smallestcoord) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y+i][x-i][0] != "b" and board[y+i][x-i][0] != "B") or (colour == "white" and board[y+i][x-i][0] != "w" and board[y+i][x-i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y+i][x-i][0] == "w" or board[y+i][x-i][0] == "W")) or (colour == "white" and (board[y+i][x-i][0] == "b" or board[y+i][x-i][0] == "B")): move(x-(i-1),y+(i-1),x-i,y+i) moving = False else: move(x-(i-1),y+(i-1),x-i,y+i) choosing = False else: moving = False if i == 1: timestried += 1 elif random == 7 and (y - 1 > -1 and x - 1 > -1): if y < x: smallestcoord = y else: smallestcoord = x random = randint(1, smallestcoord) moving = True for i in range(1, random + 1): if moving == True and((colour == "black" and board[y-i][x-i][0] != "b" and board[y-i][x-i][0] != "B") or (colour == "white" and board[y-i][x-i][0] != "w" and board[y-i][x-i][0] != "W")): if testing == True: return True elif (colour == "black" and (board[y-i][x-i][0] == "w" or board[y-i][x-i][0] == "W")) or (colour == "white" and (board[y-i][x-i][0] == "b" and board[y-i][x-i][0] == "B")): move(x-(i-1),y-(i-1),x-i,y-i) moving = False else: move(x-(i-1),y-(i-1),x-i,y-i) choosing = False else: moving = False if i == 1: timestried += 1 else: timestried += 1 if timestried >= 20 and testing == True: return False def test(x,y,colour): piece = board[y][x] if piece == "bP" or piece == "wP": return pawn(x,y,colour,True) elif piece == "bB" or piece == "wB": return bishop(x,y,colour,True) elif piece == "bK" or piece == "wK": return knight(x,y,colour,True) elif piece == "bR" or piece == "wR": return rook(x,y,colour,True) elif piece == "bQ" or piece == "wQ": return queen(x,y,colour,True) elif piece == "BK" or piece == "WK": return king(x,y,colour,True) def movepiece(x,y,colour): piece = board[y][x] if piece == "bP" or piece == "wP": pawn(x,y,colour,False) elif piece == "bB" or piece == "wB": bishop(x,y,colour,False) elif piece == "bK" or piece == "wK": knight(x,y,colour,False) elif piece == "bR" or piece == "wR": rook(x,y,colour,False) elif piece == "bQ" or piece == "wQ": queen(x,y,colour,False) elif piece == "BK" or piece == "WK": king(x,y,colour,False) def turn(colour): choosing = True stalemoves = 0 while choosing == True: x = randint(0,7) y = randint(0,7) pieceColour = board[y][x][0] if (colour == "white" and (pieceColour == "w" or pieceColour == "W")) or (colour == "black" and (pieceColour == "b" or pieceColour == "B")): if test(x,y,colour) == True: choosing = False movepiece(x,y,colour) return True else: stalemoves += 1 if stalemoves == 30: return False def turnspair(): if turn("white") == True: if turn("black") == True: global drawmoves drawmoves += 1 return True else: return False else: return False def onboard(piece): for i in range(0,8): for j in range(0,8): if board[j][i] == piece: return True return False shortest = 1000000000000 longest = 0 gamemoves = 0 def game(): playing = True global gamemoves gamemoves = 0 while playing == True: if turnspair() == True: global totalmoves totalmoves += 1 gamemoves += 1 if ((drawmoves >= 50) or (onboard("BK") == False) or (onboard("WK") == False)): playing = False else: return("DRAW") if drawmoves >= 50: return("DRAW") elif onboard("BK") == False: return("WHITE WINS") elif onboard("WK") == False: return("BLACK WINS") else: return("urmmmmmmmmm") times = int(input("how many games?")) whitewins = 0 blackwins = 0 draws = 0 for i in range(0,times): board = [["bR","bK","bB","bQ","BK","bB","bK","bR"], ["bP","bP","bP","bP","bP","bP","bP","bP"], ["__","__","__","__","__","__","__","__"], ["__","__","__","__","__","__","__","__"], ["__","__","__","__","__","__","__","__"], ["__","__","__","__","__","__","__","__"], ["wP","wP","wP","wP","wP","wP","wP","wP"], ["wR","wK","wB","wQ","WK","wB","wK","wR"]] drawmoves = 0 result = game() if gamemoves > longest: longest = gamemoves if gamemoves < shortest: shortest = gamemoves print("Games done: " + str(i + 1)) if result == "DRAW": draws += 1 elif result == "WHITE WINS": whitewins += 1 elif result == "BLACK WINS": blackwins += 1 import csv def read(filename): #subprogramme for reading from a file data = [] with open(filename) as csvfile: readCSV = csv.reader(csvfile, delimiter=",") for row in readCSV: data.append(row) return data def write(filename, data): #subprogramme for writing to a file with open(filename, "w", newline="") as csvfile: writeCSV = csv.writer(csvfile) for item in range(len(data)): writeCSV.writerow(data[item]) def add(filename, data): #subprogramme for adding to a file. Uses read and write subprogrammes file = read(filename) for item in range(len(data)): file.append(data[item]) write(filename, file) print("Black won " + str(blackwins) + " times, white won " + str(whitewins) + " times") print("There were " + str(draws) + " draws.") chessTotals = read("chessTotals.csv") totalblack = int(chessTotals[0][0]) totalblack += blackwins totalwhite = int(chessTotals[0][1]) totalwhite += whitewins totaldraw = int(chessTotals[0][2]) totaldraw += draws overallmove = int(chessTotals[0][3]) overallmove += totalmoves totaltimes = int(chessTotals[0][4]) totaltimes += times shortesttotal = int(chessTotals[0][5]) if shortest < shortesttotal: shortesttotal = shortest longesttotal = int(chessTotals[0][6]) if longest > longesttotal: longesttotal = longest blackpercent = (totalblack / totaltimes) * 100 whitepercent = (totalwhite / totaltimes) * 100 movesaverage = (overallmove / totaltimes) drawpercent = (totaldraw / totaltimes) * 100 print("Black won " + str(blackpercent) + "% of the time. White won " + str(whitepercent) + "% of the time. It is a draw " + str(drawpercent) + "% of the time") print("There were " + str(overallmove) + " moves, an average of " + str(movesaverage)) print("There have been " + str(totaltimes) + " games.") print("The longest game took " + str(longesttotal) + " turns. The shortest took " + str(shortesttotal) + " turns.") chessTotals = [[totalblack,totalwhite,totaldraw,overallmove, totaltimes, shortesttotal, longesttotal]] write("chessTotals.csv", chessTotals)