Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tictactoe: minor formatting and imports #287

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions tictactoe/TicTacToeLogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@
Based on the board for the game of Othello by Eric P. Nichols.

'''


# from bkcharts.attributes import color
class Board():

# list of all 8 directions on the board, as (x,y) offsets
__directions = [(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1)]
__directions = [(1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)]

def __init__(self, n=3):
"Set up initial board configuration."

self.n = n
# Create the empty board array.
self.pieces = [None]*self.n
self.pieces = [None] * self.n
for i in range(self.n):
self.pieces[i] = [0]*self.n
self.pieces[i] = [0] * self.n

# add [][] indexer syntax to the Board
def __getitem__(self, index):
def __getitem__(self, index):
return self.pieces[index]

def get_legal_moves(self, color):
Expand All @@ -43,18 +44,18 @@ def get_legal_moves(self, color):
# Get all the empty squares (color==0)
for y in range(self.n):
for x in range(self.n):
if self[x][y]==0:
newmove = (x,y)
if self[x][y] == 0:
newmove = (x, y)
moves.add(newmove)
return list(moves)

def has_legal_moves(self):
for y in range(self.n):
for x in range(self.n):
if self[x][y]==0:
if self[x][y] == 0:
return True
return False

def is_win(self, color):
"""Check whether the given player has collected a triplet in any direction;
@param color (1=white,-1=black)
Expand All @@ -64,42 +65,41 @@ def is_win(self, color):
for y in range(self.n):
count = 0
for x in range(self.n):
if self[x][y]==color:
if self[x][y] == color:
count += 1
if count==win:
if count == win:
return True
# check x-strips
for x in range(self.n):
count = 0
for y in range(self.n):
if self[x][y]==color:
if self[x][y] == color:
count += 1
if count==win:
if count == win:
return True
# check two diagonal strips
count = 0
for d in range(self.n):
if self[d][d]==color:
if self[d][d] == color:
count += 1
if count==win:
if count == win:
return True
count = 0
for d in range(self.n):
if self[d][self.n-d-1]==color:
if self[d][self.n - d - 1] == color:
count += 1
if count==win:
if count == win:
return True

return False

def execute_move(self, move, color):
"""Perform the given move on the board;
color gives the color pf the piece to play (1=white,-1=black)
"""

(x,y) = move
(x, y) = move

# Add the piece to the empty square.
assert self[x][y] == 0
self[x][y] = color

14 changes: 8 additions & 6 deletions tictactoe/TicTacToePlayers.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import numpy as np

"""
Random and Human-ineracting players for the game of TicTacToe.
Random and Human-interacting players for the game of TicTacToe.

Author: Evgeny Tyurin, github.com/evg-tyurin
Date: Jan 5, 2018.

Based on the OthelloPlayers by Surag Nair.

"""


class RandomPlayer():
def __init__(self, game):
self.game = game

def play(self, board):
a = np.random.randint(self.game.getActionSize())
valids = self.game.getValidMoves(board, 1)
while valids[a]!=1:
while valids[a] != 1:
a = np.random.randint(self.game.getActionSize())
return a

Expand All @@ -30,15 +32,15 @@ def play(self, board):
valid = self.game.getValidMoves(board, 1)
for i in range(len(valid)):
if valid[i]:
print(int(i/self.game.n), int(i%self.game.n))
while True:
print(int(i / self.game.n), int(i % self.game.n))
while True:
# Python 3.x
a = input()
# Python 2.x
# a = raw_input()

x,y = [int(x) for x in a.split(' ')]
a = self.game.n * x + y if x!= -1 else self.game.n ** 2
x, y = [int(x) for x in a.split(' ')]
a = self.game.n * x + y if x != -1 else self.game.n ** 2
if valid[a]:
break
else:
Expand Down
8 changes: 3 additions & 5 deletions tictactoe/keras/TicTacToeNNet.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import sys
sys.path.append('..')
from utils import *

import argparse
from tensorflow.keras.models import *
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import *
from keras.models import *
from keras.layers import *
from keras.optimizers import *

"""
NeuralNet for the game of TicTacToe.
Expand Down