Added menu to tictactoe program
This commit is contained in:
parent
31105416bd
commit
8368c2e76d
1 changed files with 25 additions and 8 deletions
33
tictactoe.py
33
tictactoe.py
|
@ -86,23 +86,24 @@ class AI(Player):
|
|||
#Check for win chance
|
||||
for x,y in board.get_empty_fields():
|
||||
boardcopy.set(x,y,self)
|
||||
# Check for win
|
||||
if boardcopy.get_winner() == self:
|
||||
return (x,y)
|
||||
|
||||
# Check for chance
|
||||
chance = self.count_win_chances(boardcopy) - enemy.count_win_chances(boardcopy)
|
||||
|
||||
moves.append( (chance,(x,y)) )
|
||||
# Check for win
|
||||
if boardcopy.get_winner() == self:
|
||||
chance += 1000
|
||||
|
||||
|
||||
|
||||
# Check if enemy win
|
||||
boardcopy.set(x,y,enemy)
|
||||
|
||||
if boardcopy.get_winner() == enemy:
|
||||
return (x,y)
|
||||
chance += 500
|
||||
|
||||
boardcopy.set(x,y,Board.empty)
|
||||
moves.append( (chance,(x,y)) )
|
||||
|
||||
moves.sort(key=lambda x: -x[0])
|
||||
|
||||
|
@ -119,7 +120,7 @@ class AI(Player):
|
|||
class Human(Player):
|
||||
def play(self, enemy, board):
|
||||
while True:
|
||||
s = input("Player %s's turn:")
|
||||
s = input("Player %s's turn:" % self.rep)
|
||||
try:
|
||||
(x,y) = map(int, s.split(","))
|
||||
return (x,y)
|
||||
|
@ -129,9 +130,25 @@ class Human(Player):
|
|||
def rotate(l):
|
||||
return l[1:] + l[:1]
|
||||
|
||||
def menu(prompt,choices):
|
||||
print("> %s" % prompt)
|
||||
for i,choice in enumerate(choices):
|
||||
print("%d. %s" % (i,choice))
|
||||
return int(input("Your choice: "))
|
||||
|
||||
if __name__ == "__main__":
|
||||
board = Board(size=6)
|
||||
players = [ AI("X"), AI("O") ]
|
||||
c = menu("Playmode",["Ai vs Ai","Ai vs Player","Player vs Player"])
|
||||
if c == 0:
|
||||
players = [ AI("X"), AI("O") ]
|
||||
elif c == 1:
|
||||
players = [ Human("X"), AI("O") ]
|
||||
elif c == 2:
|
||||
players = [ Human("X"), Human("O") ]
|
||||
else:
|
||||
import sys
|
||||
sys.exit(0)
|
||||
|
||||
board = Board(size=3)
|
||||
|
||||
print(str(board))
|
||||
|
||||
|
|
Loading…
Reference in a new issue