Compare commits

...

13 Commits

Author SHA1 Message Date
Paulina Nowak
2531f64f9c Added a possibility of specifying a player's type (random/human). 2021-01-09 22:05:47 +01:00
Paulina Nowak
4c0758aab6 Improved run function 2020-12-31 21:10:33 +01:00
Paulina Nowak
18b1305ad5 Divided broken classes into separate files 2020-12-28 22:39:37 +01:00
a376653f27 Resolved merge issues 2020-12-25 04:59:32 +01:00
Paulina Nowak
17ebde7fab asaaaaas 2020-12-25 04:45:14 +01:00
Paulina Nowak
b8dcadbeb3 asasa 2020-12-25 04:20:54 +01:00
Paulina Nowak
24213bb713 dsds 2020-12-24 06:27:20 +01:00
Paulina Nowak
a3ad4bf7ac sdsd 2020-12-24 06:10:53 +01:00
Paulina Nowak
24601be13c ffss 2020-12-24 00:39:08 +01:00
Paulina Nowak
d6ce3d2579 dupa 2020-12-24 00:33:54 +01:00
Paulina Nowak
a38a3a3d3d dsds 2020-12-23 23:37:04 +01:00
Paulina Nowak
b7b599e295 :3 2020-12-23 21:46:09 +01:00
Paulina Nowak
71d34178f0 DZIALA 2020-12-23 21:00:38 +01:00
6 changed files with 139 additions and 73 deletions

36
Board.py Normal file
View 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
View 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
View 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
View File

@ -0,0 +1,4 @@
from Player import Player
class RandomPlayer(Player):
pass

42
TicTacToe.py Normal file
View 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
View File

@ -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()