Działający PoC
Co-authored-by: Paulina Nowak <paulinq@MacBook-Air.local> Reviewed-on: #1 Co-authored-by: pnowak <251002@student.pwr.edu.pl> Co-committed-by: pnowak <251002@student.pwr.edu.pl>
This commit is contained in:
parent
5944ed9a4e
commit
7d59652863
60
main.py
60
main.py
@ -6,22 +6,32 @@
|
|||||||
| |
|
| |
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from random import randint
|
import random
|
||||||
from random import choice
|
|
||||||
|
|
||||||
|
|
||||||
class TicTacToe:
|
class TicTacToe:
|
||||||
"""
|
def __init__(self):
|
||||||
uruchamia program, gracze wykonuja na zmiane ruchy,
|
self.player1 = Player("X")
|
||||||
az do osiagniecia wygranej przez jednego z nich
|
self.player2 = Player("O")
|
||||||
"""
|
self.board = Board()
|
||||||
|
|
||||||
def run(self):
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
class Board:
|
class Board:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.state = list(" "*9)
|
self.state = list(" "*9)
|
||||||
|
self.emptyFields = [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
||||||
|
|
||||||
def print(self):
|
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"
|
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"
|
||||||
@ -30,9 +40,26 @@ class Board:
|
|||||||
def make_move(self, token, field):
|
def make_move(self, token, field):
|
||||||
if self.state[field] == " ":
|
if self.state[field] == " ":
|
||||||
self.state[field] = token
|
self.state[field] = token
|
||||||
|
self.emptyFields.remove(field)
|
||||||
else:
|
else:
|
||||||
print("This field's taken.")
|
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():
|
def get_state():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -43,23 +70,12 @@ class Player:
|
|||||||
def __init__(self, token):
|
def __init__(self, token):
|
||||||
self.token = token
|
self.token = token
|
||||||
|
|
||||||
def make_random_move(self, state):
|
def make_random_move(self, emptyFields):
|
||||||
|
return random.choice(emptyFields)
|
||||||
emptyField = True
|
|
||||||
while emptyField:
|
|
||||||
field = state[randint(0,8)]
|
|
||||||
if field == " ":
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
player1 = Player("X")
|
|
||||||
player2 = Player("O")
|
|
||||||
|
|
||||||
game = TicTacToe()
|
game = TicTacToe()
|
||||||
game.run()
|
game.run()
|
||||||
|
|
||||||
for x in range(1,10):
|
|
||||||
player.make_random_move(game.state)
|
|
||||||
game.print()
|
|
||||||
player1.make_random_move(game.state)
|
|
||||||
game.print()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user