develop #2
47
main.py
47
main.py
@ -6,37 +6,32 @@
|
|||||||
| |
|
| |
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from random import randint
|
import random
|
||||||
|
|
||||||
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):
|
||||||
|
|||||||
player1 = Player("X")
|
while True:
|
||||||
player2 = Player("O")
|
self.board.make_move(self.player1.token, self.player1.make_random_move(self.board.emptyFields))
|
||||||
|
self.board.print()
|
||||||
board = Board()
|
if self.board.isWin(self.player1.token):
|
||||||
|
|
||||||
for x in range(1,10):
|
|
||||||
board.make_move(player1.token, player1.make_random_move(board.state))
|
|
||||||
board.print()
|
|
||||||
if board.isWin(player1.token):
|
|
||||||
print("Player 1 has won.")
|
print("Player 1 has won.")
|
||||||
break
|
break
|
||||||
|
|
||||||
board.make_move(player2.token, player2.make_random_move(board.state))
|
self.board.make_move(self.player2.token, self.player2.make_random_move(self.board.emptyFields))
|
||||||
board.print()
|
self.board.print()
|
||||||
if board.isWin(player2.token):
|
if self.board.isWin(self.player2.token):
|
||||||
print("Player 2 has won.")
|
print("Player 2 has won.")
|
||||||
break
|
break
|
||||||
|
|
||||||
class Board:
|
class Board:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.state = list(" "*9)
|
self.state = list(" "*9)
|
||||||
swarga
commented
To możemy zastąpić generacją listy. To możemy zastąpić generacją listy.
|
|||||||
|
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"
|
||||||
@ -45,9 +40,14 @@ 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
|
||||||
swarga
commented
Good. Good.
|
|||||||
|
self.emptyFields.remove(field)
|
||||||
else:
|
else:
|
||||||
print("This field's taken.")
|
print("This field's taken.")
|
||||||
|
|
||||||
swarga
commented
Poczytaj o enumie. Poczytaj o enumie.
Plasznę możemy reprezentować jako listę intów: 0, -1 oraz +1.
Pomyśl w jaki sposób może to ułatwić znajdywanie wygranej (modulo is your friend).
|
|||||||
|
"""
|
||||||
|
Ta funkcja byłaby dużo bardziej użyteczna,
|
||||||
|
gdyby informowała nas który z graczy jest zwycięzcą
|
||||||
|
"""
|
||||||
def isWin(self, token):
|
def isWin(self, token):
|
||||||
if((self.state[0] == token and self.state[1] == token and self.state[2] == token) or
|
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[4] == token and self.state[8] == token) or
|
||||||
@ -69,16 +69,9 @@ class Board:
|
|||||||
class Player:
|
class Player:
|
||||||
def __init__(self, token):
|
def __init__(self, token):
|
||||||
self.token = token
|
self.token = token
|
||||||
|
|
||||||
|
|
||||||
def make_random_move(self, state):
|
|
||||||
|
|
||||||
emptyField = True
|
def make_random_move(self, emptyFields):
|
||||||
while emptyField:
|
return random.choice(emptyFields)
|
||||||
swarga
commented
Jeden tab za dużo? Jeden tab za dużo?
|
|||||||
randomField = randint(0,8)
|
|
||||||
field = state[randomField]
|
|
||||||
if field == " ":
|
|
||||||
return randomField
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user
Ten cały blok kodu możnaby sprowadzić do: