Divided broken classes into separate files
This commit is contained in:
parent
a376653f27
commit
18b1305ad5
30
Board.py
Normal file
30
Board.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
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)):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_state(field):
|
||||||
|
return self.state[field]
|
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_random_field(self, empty_fields):
|
||||||
|
return random.choice(empty_fields)
|
57
TicTacToe.py
Normal file
57
TicTacToe.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
from Player import Player
|
||||||
|
from Board import Board
|
||||||
|
|
||||||
|
"""
|
||||||
|
Gracz wykonuje ruch.
|
||||||
|
Sprawdzamy, czy mamy wygranego (remis).
|
||||||
|
Zmieniamy gracza.
|
||||||
|
Wracamy do 1.
|
||||||
|
`while noWinner():
|
||||||
|
player.make_move()
|
||||||
|
isWin()
|
||||||
|
swapPlayers()
|
||||||
|
`
|
||||||
|
|
||||||
|
Coś takiego
|
||||||
|
|
||||||
|
Twój kod dwa razy robi rzeczy
|
||||||
|
|
||||||
|
Zamiast zamienić gracza
|
||||||
|
|
||||||
|
I zrobić to raz
|
||||||
|
|
||||||
|
Linie 20-27 sa te same co 29-36
|
||||||
|
|
||||||
|
Tylko tam jest player 2
|
||||||
|
"""
|
||||||
|
|
||||||
|
class TicTacToe:
|
||||||
|
def __init__(self):
|
||||||
|
self.player1 = Player("X", "Player 1")
|
||||||
|
self.player2 = Player("O", "Player 2")
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.board = Board()
|
||||||
|
while True:
|
||||||
|
if len(self.board.empty_fields) == 0:
|
||||||
|
self.board.print()
|
||||||
|
print("It's a draw.")
|
||||||
|
break
|
||||||
|
|
||||||
|
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()
|
||||||
|
print(self.player1.name + " has won.\n")
|
||||||
|
break
|
||||||
|
|
||||||
|
if len(self.board.empty_fields) == 0:
|
||||||
|
self.board.print()
|
||||||
|
print("It's a draw.")
|
||||||
|
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()
|
||||||
|
print(self.player2.name + " has won.\n")
|
||||||
|
break
|
||||||
|
|
80
main.py
80
main.py
@ -1,83 +1,5 @@
|
|||||||
"""
|
|
||||||
| |
|
|
||||||
-|-|-
|
|
||||||
| |
|
|
||||||
-|-|-
|
|
||||||
| |
|
|
||||||
|
|
||||||
"""
|
|
||||||
import random
|
|
||||||
import sys, getopt
|
import sys, getopt
|
||||||
|
from TicTacToe import TicTacToe
|
||||||
class TicTacToe:
|
|
||||||
def __init__(self):
|
|
||||||
self.player1 = Player("X", "Player 1")
|
|
||||||
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
|
number_of_rounds = None
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user