Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
2531f64f9c | ||
|
4c0758aab6 | ||
|
18b1305ad5 | ||
a376653f27 | |||
|
17ebde7fab | ||
|
b8dcadbeb3 | ||
|
24213bb713 | ||
|
a3ad4bf7ac | ||
|
24601be13c | ||
|
d6ce3d2579 | ||
|
a38a3a3d3d | ||
|
b7b599e295 | ||
|
71d34178f0 |
36
Board.py
Normal file
36
Board.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
class Board:
|
||||||
|
def __init__(self):
|
||||||
|
self.state = list(" "*9)
|
||||||
|
self.empty_fields = [x for x in range(9)]
|
||||||
|
|
||||||
|
def print(self):
|
||||||
|
board = ""+ self.state[0] +"|"+self.state[1] +"|"+self.state[2] +" \n-|-|-\n"+self.state[3] +"|"+self.state[4] +"|"+self.state[5] +"\n-|-|-\n"+self.state[6] +"|"+self.state[7] +"|"+self.state[8] +"\n"
|
||||||
|
print(board)
|
||||||
|
|
||||||
|
def make_move(self, token, field):
|
||||||
|
if self.state[field] == " ":
|
||||||
|
self.state[field] = token
|
||||||
|
self.empty_fields.remove(field)
|
||||||
|
else:
|
||||||
|
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
|
||||||
|
(self.state[0] == token and self.state[4] == token and self.state[8] == token) or
|
||||||
|
(self.state[0] == token and self.state[3] == token and self.state[6] == token) or
|
||||||
|
(self.state[1] == token and self.state[4] == token and self.state[7] == token) or
|
||||||
|
(self.state[2] == token and self.state[5] == token and self.state[8] == token) or
|
||||||
|
(self.state[2] == token and self.state[4] == token and self.state[6] == token) or
|
||||||
|
(self.state[3] == token and self.state[4] == token and self.state[5] == token) or
|
||||||
|
(self.state[6] == token and self.state[7] == token and self.state[8] == token)):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_state(field):
|
||||||
|
return self.state[field]
|
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())
|
9
Player.py
Normal file
9
Player.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
class Player:
|
||||||
|
def __init__(self, token, name):
|
||||||
|
self.token = token
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
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
|
42
TicTacToe.py
Normal file
42
TicTacToe.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
from Player import Player
|
||||||
|
from RandomPlayer import RandomPlayer
|
||||||
|
from HumanPlayer import HumanPlayer
|
||||||
|
from Board import Board
|
||||||
|
|
||||||
|
class TicTacToe:
|
||||||
|
def __init__(self, player_x, player_o):
|
||||||
|
self.players = []
|
||||||
|
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_field(self.board.empty_fields))
|
||||||
|
self.board.print()
|
||||||
|
|
||||||
|
if self.board.is_win(self.players[player].token):
|
||||||
|
print(self.players[player].name + " has won.\n")
|
||||||
|
break
|
||||||
|
|
||||||
|
if player == 0:
|
||||||
|
player = 1
|
||||||
|
else:
|
||||||
|
player = 0
|
||||||
|
else:
|
||||||
|
print("It's a draw.")
|
121
main.py
121
main.py
@ -1,99 +1,48 @@
|
|||||||
"""
|
|
||||||
| |
|
|
||||||
-|-|-
|
|
||||||
| |
|
|
||||||
-|-|-
|
|
||||||
| |
|
|
||||||
|
|
||||||
"""
|
|
||||||
import random
|
|
||||||
import sys, getopt
|
import sys, getopt
|
||||||
|
from TicTacToe import TicTacToe
|
||||||
|
|
||||||
class TicTacToe:
|
games = None
|
||||||
def __init__(self):
|
player_x = None
|
||||||
self.player1 = Player("X", "Player 1")
|
player_o = None
|
||||||
self.player2 = Player("O", "Player 2")
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
self.board = Board()
|
|
||||||
while True:
|
|
||||||
self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.empty_fields))
|
|
||||||
if self.board.is_win(self.player1.token):
|
|
||||||
self.board.print()
|
|
||||||
if len(self.board.empty_fields) == 0:
|
|
||||||
print("It's a draw.\n")
|
|
||||||
else:
|
|
||||||
print(self.player1.name + " has won.\n")
|
|
||||||
break
|
|
||||||
|
|
||||||
self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.empty_fields))
|
|
||||||
if self.board.is_win(self.player2.token):
|
|
||||||
self.board.print()
|
|
||||||
if len(self.board.empty_fields) == 0:
|
|
||||||
print("It's a draw.\n")
|
|
||||||
else:
|
|
||||||
print(self.player2.name + " has won.\n")
|
|
||||||
break
|
|
||||||
|
|
||||||
class Board:
|
|
||||||
def __init__(self):
|
|
||||||
self.state = list(" "*9)
|
|
||||||
self.empty_fields = [x for x in range(9)]
|
|
||||||
|
|
||||||
def print(self):
|
|
||||||
board = ""+ self.state[0] +"|"+self.state[1] +"|"+self.state[2] +" \n-|-|-\n"+self.state[3] +"|"+self.state[4] +"|"+self.state[5] +"\n-|-|-\n"+self.state[6] +"|"+self.state[7] +"|"+self.state[8] +"\n"
|
|
||||||
print(board)
|
|
||||||
|
|
||||||
def make_move(self, token, field):
|
|
||||||
if self.state[field] == " ":
|
|
||||||
self.state[field] = token
|
|
||||||
self.empty_fields.remove(field)
|
|
||||||
else:
|
|
||||||
print("This field's taken.")
|
|
||||||
|
|
||||||
def is_win(self, token):
|
|
||||||
if((self.state[0] == token and self.state[1] == token and self.state[2] == token) or
|
|
||||||
(self.state[0] == token and self.state[4] == token and self.state[8] == token) or
|
|
||||||
(self.state[0] == token and self.state[3] == token and self.state[6] == token) or
|
|
||||||
(self.state[1] == token and self.state[4] == token and self.state[7] == token) or
|
|
||||||
(self.state[2] == token and self.state[5] == token and self.state[8] == token) or
|
|
||||||
(self.state[2] == token and self.state[4] == token and self.state[6] == token) or
|
|
||||||
(self.state[3] == token and self.state[4] == token and self.state[5] == token) or
|
|
||||||
(self.state[6] == token and self.state[7] == token and self.state[8] == token) or
|
|
||||||
len(self.empty_fields) == 0):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def get_state():
|
|
||||||
pass
|
|
||||||
|
|
||||||
def get_action():
|
|
||||||
pass
|
|
||||||
|
|
||||||
class Player:
|
|
||||||
def __init__(self, token, name):
|
|
||||||
self.token = token
|
|
||||||
self.name = name
|
|
||||||
|
|
||||||
def choose_random_field(self, empty_fields):
|
|
||||||
return random.choice(empty_fields)
|
|
||||||
|
|
||||||
|
|
||||||
number_of_rounds = None
|
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="])
|
opts, args = getopt.getopt(sys.argv[1:], "n:", ["games="])
|
||||||
except getopt.GetoptError:
|
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)
|
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:
|
for opt, arg in opts:
|
||||||
if opt in ("-n", "--numberofrounds"):
|
if opt in ("-n", "--numberofrounds"):
|
||||||
number_of_rounds = arg
|
games = arg
|
||||||
|
player_x = sys.argv[-2]
|
||||||
|
player_o = sys.argv[-1]
|
||||||
else:
|
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()
|
sys.exit()
|
||||||
|
|
||||||
game = TicTacToe()
|
game = TicTacToe(player_x, player_o)
|
||||||
for x in range(0, int(number_of_rounds)):
|
for x in range(0, int(games)):
|
||||||
game.run()
|
game.run()
|
Loading…
Reference in New Issue
Block a user