Added a possibility of specifying a player's type (random/human).
This commit is contained in:
parent
4c0758aab6
commit
2531f64f9c
8
Board.py
8
Board.py
@ -12,7 +12,13 @@ class Board:
|
||||
self.state[field] = token
|
||||
self.empty_fields.remove(field)
|
||||
else:
|
||||
print("This field's taken.")
|
||||
print("This field's already taken. Choose another one.")
|
||||
while field not in self.empty_fields:
|
||||
field = int(input())
|
||||
if field not in self.empty_fields:
|
||||
print("This field's already taken. Choose another one.")
|
||||
self.state[field] = token
|
||||
self.empty_fields.remove(field)
|
||||
|
||||
def is_win(self, token):
|
||||
if((self.state[0] == token and self.state[1] == token and self.state[2] == token) or
|
||||
|
8
HumanPlayer.py
Normal file
8
HumanPlayer.py
Normal file
@ -0,0 +1,8 @@
|
||||
from Player import Player
|
||||
|
||||
class HumanPlayer(Player):
|
||||
def __init__(self, token, name):
|
||||
Player.__init__(self, token, name)
|
||||
|
||||
def choose_field(self, empty_fields):
|
||||
return int(input())
|
@ -3,7 +3,7 @@ import random
|
||||
class Player:
|
||||
def __init__(self, token, name):
|
||||
self.token = token
|
||||
self.name = name
|
||||
self.name = name
|
||||
|
||||
def choose_random_field(self, empty_fields):
|
||||
return random.choice(empty_fields)
|
||||
def choose_field(self, empty_fields):
|
||||
return random.choice(empty_fields)
|
||||
|
4
RandomPlayer.py
Normal file
4
RandomPlayer.py
Normal file
@ -0,0 +1,4 @@
|
||||
from Player import Player
|
||||
|
||||
class RandomPlayer(Player):
|
||||
pass
|
32
TicTacToe.py
32
TicTacToe.py
@ -1,26 +1,42 @@
|
||||
from Player import Player
|
||||
from RandomPlayer import RandomPlayer
|
||||
from HumanPlayer import HumanPlayer
|
||||
from Board import Board
|
||||
|
||||
class TicTacToe:
|
||||
def __init__(self):
|
||||
def __init__(self, player_x, player_o):
|
||||
self.players = []
|
||||
self.players.append( Player("X", "Player 1") )
|
||||
self.players.append( Player("O", "Player 2") )
|
||||
self.player_x = player_x
|
||||
self.player_o = player_o
|
||||
|
||||
if self.player_x == "random":
|
||||
self.players.append(RandomPlayer("X", "Player X"))
|
||||
else:
|
||||
self.players.append(HumanPlayer("X", "Player X"))
|
||||
|
||||
if self.player_o == "random":
|
||||
self.players.append(RandomPlayer("O", "Player O"))
|
||||
else:
|
||||
self.players.append(HumanPlayer("O", "Player O"))
|
||||
|
||||
def run(self):
|
||||
self.board = Board()
|
||||
player = 0
|
||||
|
||||
if self.player_x == "human":
|
||||
self.board.print()
|
||||
|
||||
while len(self.board.empty_fields) != 0:
|
||||
self.board.make_move(self.players[player].token, self.players[player].choose_random_field(self.board.empty_fields))
|
||||
self.board.make_move(self.players[player].token, self.players[player].choose_field(self.board.empty_fields))
|
||||
self.board.print()
|
||||
|
||||
if self.board.is_win(self.players[player].token):
|
||||
self.board.print()
|
||||
print(self.players[player].name + " has won.\n")
|
||||
break
|
||||
if (player == 0):
|
||||
|
||||
if player == 0:
|
||||
player = 1
|
||||
else:
|
||||
player = 0
|
||||
else:
|
||||
self.board.print()
|
||||
print("It's a draw.\n")
|
||||
print("It's a draw.")
|
43
main.py
43
main.py
@ -1,21 +1,48 @@
|
||||
import sys, getopt
|
||||
from TicTacToe import TicTacToe
|
||||
|
||||
number_of_rounds = None
|
||||
games = None
|
||||
player_x = None
|
||||
player_o = None
|
||||
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="])
|
||||
opts, args = getopt.getopt(sys.argv[1:], "n:", ["games="])
|
||||
except getopt.GetoptError:
|
||||
print("main.py -n <numberofrounds>")
|
||||
print('''
|
||||
usage: main.py -n GAMES
|
||||
player_x player_o
|
||||
|
||||
positional arguments:
|
||||
player_x Type of the X player [human|random]
|
||||
player_o Type of the O player [human|random]
|
||||
|
||||
optional arguments:
|
||||
-n GAMES, --games GAMES
|
||||
number of games to play
|
||||
''')
|
||||
sys.exit(2)
|
||||
|
||||
if(len(sys.argv) != 1):
|
||||
if((len(sys.argv) != 1) and (sys.argv[-2] in ("random", "human")) and (sys.argv[-1] in ("random", "human"))):
|
||||
for opt, arg in opts:
|
||||
if opt in ("-n", "--numberofrounds"):
|
||||
number_of_rounds = arg
|
||||
games = arg
|
||||
player_x = sys.argv[-2]
|
||||
player_o = sys.argv[-1]
|
||||
else:
|
||||
print("main.py -n <numberofrounds>")
|
||||
print('''
|
||||
usage: main.py -n GAMES
|
||||
player_x player_o
|
||||
|
||||
positional arguments:
|
||||
player_x Type of the X player [human|random]
|
||||
player_o Type of the O player [human|random]
|
||||
|
||||
optional arguments:
|
||||
-n GAMES, --games GAMES
|
||||
number of games to play
|
||||
''')
|
||||
sys.exit()
|
||||
|
||||
game = TicTacToe()
|
||||
for x in range(0, int(number_of_rounds)):
|
||||
game = TicTacToe(player_x, player_o)
|
||||
for x in range(0, int(games)):
|
||||
game.run()
|
Loading…
Reference in New Issue
Block a user