def __init__(self): self.board = np.full((3, 3), " ") self.gameRunning = True self.mover = 0 self.p1Start = 0 self.p2Start = 0 self.totalGames = 0 self.p1Win = 0 self.p2Win = 0 self.tie = 0 self.player = mp.machinePlayer() self.rdPlayer = rp.randomPlayer([(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]) self.rdPlayer2 = rp.randomPlayer([(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)])
def __init__(self, playername): self.player = randomPlayer.randomPlayer() if playername == 'random': self.player = randomPlayer.randomPlayer()
def main(): global LINE PLAY_ORDER = [1, 2] g = xo(3, 3, 2, ['X', 'O'], PLAY_ORDER) name = input("Player 1 - Enter your name :") player1 = humanPlayer(1, name, g) player2 = randomPlayer(2, "Random Player", g) print('\nTic Tac Toe Platform\n' + player1.title + ' vs ' + player2.title) console_log(0, LINE) rand1_win = 0 rand1_win_turns = 0 rand2_win = 0 rand2_win_turns = 0 draw = 0 f = open('game_log_' + player1.name + '-vs-' + player2.name + '.csv', 'w') # Play N Games for i in range(0, N_GAMES): csv_line = '' g.reset() current_winner = 0 # create a game instance if ROLL: PLAY_ORDER = roll(PLAY_ORDER, 1) # Keep playing till the game in not over. while (g.game_over == False): while True: move = player1.play() result = g.mark(move, 1) console_log(3, 'Result: ' + str(result) + "\n") if (not ((result == err.INVALID_MOVE) | (result == err.OUT_OF_TURN))): break if (result == err.DRAW): console_log(2, g.get_board()) current_winner = 0 draw += 1 if (result == err.WIN): console_log(2, '______________________') console_log(2, g.get_board()) console_log(1, player1.name + ' has won') current_winner = 1 console_log(2, '______________________') rand1_win += 1 rand1_win_turns += g.game_turn if (g.game_over == False): while True: move = player2.play() result = g.mark(move, 2) console_log(3, 'Result: ' + str(result) + "\n") if (not ((result == err.INVALID_MOVE) | (result == err.OUT_OF_TURN))): break if (result == err.DRAW): console_log(2, g.get_board()) current_winner = 0 draw += 1 if (result == err.WIN): console_log(2, "______________________") console_log(2, g.get_board()) console_log(1, player2.name + " has won") console_log(2, "______________________") current_winner = 2 rand2_win += 1 rand2_win_turns += g.game_turn # Log game to CSV file for i, e in enumerate(g.play_order): csv_line += str(e) + ',' csv_line += str(current_winner) for e in g.gameLog[:, 1]: csv_line += ', ' + str(e) csv_line += '\n' f.write(csv_line) f.close() console_log(1, LINE) print(player1.name + " Winner :" + str(rand1_win)) if (rand1_win > 0): print("Average Moves to win :" + str(rand1_win_turns / rand1_win)) print(player2.name + " Winner :" + str(rand2_win)) if (rand2_win > 0): print("Average Moves to win :" + str(rand2_win_turns / rand2_win)) print("Draw :" + str(draw))
#Pour vous donner une idée de comment le tournoi sera organisé, # localGame.py permet de connecter deux joueurs via leurs interfaces. # randomPlayer vs RandomPlayer import Reversi import randomPlayer import time from io import StringIO import sys b = Reversi.Board(10) players = [] player1 = randomPlayer.randomPlayer() player1.newGame(b._BLACK) players.append(player1) player2 = randomPlayer.randomPlayer() player2.newGame(b._WHITE) players.append(player2) totalTime = [0,0] # total real time for each player nextplayer = 0 nextplayercolor = b._BLACK nbmoves = 1 outputs = ["",""] sysstdout= sys.stdout stringio = StringIO() print(b.legal_moves()) while not b.is_game_over():
def main(): global LINE PLAY_ORDER = [1,2] g=xo(3,3,2,['X','O'],PLAY_ORDER) name = input("Player 1 - Enter your name :") player1 = humanPlayer(1,name,g) player2 = randomPlayer(2,"Random Player",g) print('\nTic Tac Toe Platform\n'+player1.title+' vs '+player2.title) console_log(0,LINE) rand1_win = 0 rand1_win_turns = 0 rand2_win = 0 rand2_win_turns = 0 draw = 0 f = open('game_log_'+player1.name+'-vs-'+player2.name+'.csv', 'w') # Play N Games for i in range (0, N_GAMES): csv_line = '' g.reset() current_winner = 0 # create a game instance if ROLL : PLAY_ORDER = roll (PLAY_ORDER,1) # Keep playing till the game in not over. while(g.game_over == False): while True: [move,result] = player1.play() console_log (3,'Result: '+str(result)+"\n") if (not((result == err.INVALID_MOVE) | (result == err.OUT_OF_TURN))): break if (result == err.DRAW): console_log (2, g.get_board()) current_winner = 0; draw += 1 if (result == err.WIN): console_log (2, '______________________') console_log (2, g.get_board()) console_log (1, player1.name+' has won') current_winner = 1 console_log (2, '______________________') rand1_win += 1 rand1_win_turns += g.game_turn if (g.game_over == False): while True: [move, result] = player2.play() console_log (3,'Result: '+str(result)+"\n") if (not((result == err.INVALID_MOVE) | (result == err.OUT_OF_TURN))): break if (result == err.DRAW): console_log (2,g.get_board()) current_winner = 0 draw += 1 if (result == err.WIN): console_log (2, "______________________") console_log (2, g.get_board()) console_log (1, player2.name+" has won") console_log (2, "______________________") current_winner = 2 rand2_win += 1 rand2_win_turns += g.game_turn; # Log game to CSV file for i, e in enumerate(g.play_order): csv_line += str(e)+',' csv_line += str(current_winner) for e in g.gameLog[:,1]: csv_line += ', '+str(e) csv_line += '\n' f.write(csv_line) f.close() console_log(1,LINE) print (player1.name+" Winner :"+str(rand1_win)) if (rand1_win>0): print ("Average Moves to win :"+str(rand1_win_turns/rand1_win)) print (player2.name+" Winner :"+str(rand2_win)) if (rand2_win>0): print ("Average Moves to win :"+str(rand2_win_turns/rand2_win)) print ("Draw :"+str(draw))
def main(): global LINE PLAY_ORDER = [1, 2] g = xo(3, 3, 2, ['X', 'O'], PLAY_ORDER) player1 = tdPlayer(2, "tdPlayer", g) player2 = randomPlayer(1, "Rand", g) print('\nThe XO - Tic Tac Toe Platform\n' + player1.title + ' vs ' + player2.title) console_log(0, LINE) player1_win = 0 player1_win_turns = 0 player2_win = 0 player2_win_turns = 0 draw = 0 # Play N Games csv_line = '' for i in range(0, N_GAMES): g.reset() player1.reset() player2.reset() current_winner = 0 # create a game instance if ROLL: PLAY_ORDER = roll(PLAY_ORDER, 1) # Keep playing till the game in not over. while (not g.game_over): move = player1.play() result = g.mark(move, 1) console_log(3, 'Result: ' + str(result) + '\n') if (result == err.DRAW): console_log(2, '______________________') console_log(2, g.get_board()) console_log(1, 'Game Drawn') console_log(2, '______________________') current_winner = 0 draw += 1 if (result == err.WIN): console_log(2, '______________________') console_log(2, g.get_board()) console_log(1, player1.name + ' has won') console_log(2, '______________________') current_winner = 1 player1_win += 1 player1_win_turns += g.game_turn if (not g.game_over): move = player2.play() result = g.mark(move, 2) console_log(3, 'Result: ' + str(result) + '\n') if (result == err.DRAW): console_log(2, '______________________') console_log(2, g.get_board()) console_log(1, 'Game Drawn') console_log(2, '______________________') draw += 1 current_winner = 0 draw += 1 if (result == err.WIN): console_log(2, "______________________") console_log(2, g.get_board()) console_log(1, player2.name + " has won") console_log(2, "______________________") current_winner = 2 player2_win += 1 player2_win_turns += g.game_turn # Log game to CSV file for i, e in enumerate(g.play_order): csv_line += str(e) + ',' csv_line += str(current_winner) for e in g.gameLog[:, 1]: csv_line += ', ' + str(e) csv_line += '\n' f = open('game_log_' + player1.name + '-vs-' + player2.name + '.csv', 'w') f.write(csv_line) f.close() console_log(1, '\n' + LINE) print(player1.name + ' Winner :' + str(player1_win)) if (player1_win > 0): print('Average Moves to win :' + str(player1_win_turns / player1_win)) print(player2.name + ' Winner :' + str(player2_win)) if (player2_win > 0): print("Average Moves to win :" + str(player2_win_turns / player2_win)) print('Draw :' + str(draw))
def l(): b = Reversi.Board(10) players = [] player1 = myPlayer.myPlayer() player1.newGame(b._BLACK) players.append(player1) player2 = randomPlayer.randomPlayer() player2.newGame(b._WHITE) players.append(player2) totalTime = [0, 0] # total real time for each player nextplayer = 0 nextplayercolor = b._BLACK nbmoves = 1 outputs = ["", ""] sysstdout = sys.stdout stringio = StringIO() print(b.legal_moves()) while not b.is_game_over(): #print("Referee Board:") #print(b) #print("Before move", nbmoves) #print("Legal Moves: ", b.legal_moves()) nbmoves += 1 otherplayer = (nextplayer + 1) % 2 othercolor = b._BLACK if nextplayercolor == b._WHITE else b._WHITE currentTime = time.time() sys.stdout = stringio move = players[nextplayer].getPlayerMove() sys.stdout = sysstdout playeroutput = "\r" + stringio.getvalue() stringio.truncate(0) #print(("[Player "+str(nextplayer) + "] ").join(playeroutput.splitlines(True))) outputs[nextplayer] += playeroutput totalTime[nextplayer] += time.time() - currentTime #print("Player ", nextplayercolor, players[nextplayer].getPlayerName(), "plays" + str(move)) (x, y) = move if not b.is_valid_move(nextplayercolor, x, y): #print(otherplayer, nextplayer, nextplayercolor) #print("Problem: illegal move") break b.push([nextplayercolor, x, y]) players[otherplayer].playOpponentMove(x, y) nextplayer = otherplayer nextplayercolor = othercolor #print(b) print("The game is over") print(b) (nbwhites, nbblacks) = b.get_nb_pieces() print("Time:", totalTime) print("Winner: ", end="") f = open("result.txt", "a+") if nbwhites > nbblacks: print("WHITE") f.write("Winner : White\n") elif nbblacks > nbwhites: print("BLACK") f.write("Winner : Black\n") else: print("DEUCE") f.write("Time :") for item in totalTime: f.write("%s " % item) f.write("\n") f.write("Nombre piece joueur Noir : %d\n" % nbblacks) f.write("Nombre piece joueur Blanc : %d\n" % nbwhites) f.write("\n\n") f.close()
game.make_move(0,2) print(game) print() game.make_move(2,2) print(game) print() print("Second Game") print() game2 = Game() p0 = humanPlayer() p1 = randomPlayer() players = [p0,p1] count = 0 print (game2) while True: player = players[count%2] count +=1 x,y = player.move(game2) print(x,y) is_over = game2.make_move(x,y) print(game2) print() if is_over: break print("Third Game")