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.")
|
113
main.py
113
main.py
@ -1,81 +1,48 @@
|
||||
"""
|
||||
| |
|
||||
-|-|-
|
||||
| |
|
||||
-|-|-
|
||||
| |
|
||||
import sys, getopt
|
||||
from TicTacToe import TicTacToe
|
||||
|
||||
"""
|
||||
import random
|
||||
games = None
|
||||
player_x = None
|
||||
player_o = None
|
||||
|
||||
class TicTacToe:
|
||||
def __init__(self):
|
||||
self.player1 = Player("X")
|
||||
self.player2 = Player("O")
|
||||
self.board = Board()
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], "n:", ["games="])
|
||||
except getopt.GetoptError:
|
||||
print('''
|
||||
usage: main.py -n GAMES
|
||||
player_x player_o
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
self.board.make_move(self.player1.token, self.player1.make_random_move(self.board.emptyFields))
|
||||
self.board.print()
|
||||
if self.board.isWin(self.player1.token):
|
||||
print("Player 1 has won.")
|
||||
break
|
||||
positional arguments:
|
||||
player_x Type of the X player [human|random]
|
||||
player_o Type of the O player [human|random]
|
||||
|
||||
self.board.make_move(self.player2.token, self.player2.make_random_move(self.board.emptyFields))
|
||||
self.board.print()
|
||||
if self.board.isWin(self.player2.token):
|
||||
print("Player 2 has won.")
|
||||
break
|
||||
optional arguments:
|
||||
-n GAMES, --games GAMES
|
||||
number of games to play
|
||||
''')
|
||||
sys.exit(2)
|
||||
|
||||
class Board:
|
||||
def __init__(self):
|
||||
self.state = list(" "*9)
|
||||
self.emptyFields = [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
||||
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"):
|
||||
games = arg
|
||||
player_x = sys.argv[-2]
|
||||
player_o = sys.argv[-1]
|
||||
else:
|
||||
print('''
|
||||
usage: main.py -n GAMES
|
||||
player_x player_o
|
||||
|
||||
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.emptyFields.remove(field)
|
||||
else:
|
||||
print("This field's taken.")
|
||||
|
||||
"""
|
||||
Ta funkcja byłaby dużo bardziej użyteczna,
|
||||
gdyby informowała nas który z graczy jest zwycięzcą
|
||||
"""
|
||||
def isWin(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():
|
||||
pass
|
||||
|
||||
def get_action():
|
||||
pass
|
||||
|
||||
class Player:
|
||||
def __init__(self, token):
|
||||
self.token = token
|
||||
|
||||
def make_random_move(self, emptyFields):
|
||||
return random.choice(emptyFields)
|
||||
|
||||
|
||||
|
||||
game = TicTacToe()
|
||||
game.run()
|
||||
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(player_x, player_o)
|
||||
for x in range(0, int(games)):
|
||||
game.run()
|
Loading…
Reference in New Issue
Block a user