Skip to content

Commit

Permalink
Fixed frontend error and removed debugging statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Buuntu committed Jan 24, 2016
1 parent ceb8899 commit e6b8c01
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
25 changes: 12 additions & 13 deletions game.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from copy import deepcopy
from random import randint
from sys import stderr

class Game:
'Class used to keep track of the board and calculate computer moves'
Expand All @@ -14,9 +13,8 @@ def __init__(self, player='X', computer='O'):

# Calculate computer move based on minimax algorithm
def calculate_move(self):
# if it's the computer's first turn, take one of the corners (much faster than calculating with algorithm)
# if it's the computer's first turn, take one of the corners (much faster than calculating with recursion)
if self.is_board_empty():
print("board is empty", file=stderr)
move = self.random_corner()
return {'row': move[0], 'col': move[1]}

Expand All @@ -40,19 +38,18 @@ def minimax(self, mark):
return [0, None]

if mark == self.player:
best = [-100, None]
best = [-2, None]
else:
best = [+100, None]
best = [+2, None]

# Loop through all available moves
for move in self.get_moves():
# Duplicate board and make move
new_board = Game('X', 'O')
new_board = Game(self.player, self.computer)
new_board.board = deepcopy(self.board)
new_board.move(mark, move[0], move[1])

# Recursively calculate value of next move
value = new_board.minimax(self.opponent(mark))[0]
# Make the move and recursively calculate the value of the next move
value = new_board.move(mark, move[0], move[1]).minimax(self.opponent(mark))[0]

# Player moves
if mark == self.player:
Expand All @@ -66,20 +63,22 @@ def minimax(self, mark):
return best

def opponent(self, mark):
if mark == 'X':
return 'O'
if mark == self.player:
return self.computer
else:
return 'X'
return self.player

# Gets a list of available moves
def get_moves(self):
moves = []
for r in range(3):
for c in range(3):
if self.board[r][c] == ' ':
if self.board[r][c] == self.empty:
moves.append([r, c])

return moves

# Checks if the game is tied
def tied(self):
if not self.has_won('X') and not self.has_won('Y') and self.is_board_full():
return True
Expand Down
2 changes: 1 addition & 1 deletion static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ app.controller('MainController', ['$scope', '$http', function($scope, $http) {
return [[0, 0], [1, 1], [2, 2]];
}
else if ($scope.board[2][0] == mark && ($scope.board[2][0] == $scope.board[1][1]) && ($scope.board[2][0] == $scope.board[0][2])) {
return [[2, 0], [1, 1], [2, 0]];
return [[2, 0], [1, 1], [0, 2]];
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/css/style.css">
<!--<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<!-- Angular -->
Expand Down
20 changes: 10 additions & 10 deletions tictactoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
# Tic Tac Toe game
#
# Designed so that the computer always wins or ties
# Uses the minimax algorithm to calculate the next best move
#
# Uses Flask framework and AngularJS for the frontend
# Written in Flask framework and AngularJS for the frontend
#--------------------------------------------------------------------
from flask import Flask, render_template, jsonify, request
from game import Game

app = Flask(__name__)

# Initialize board
game = Game('X', 'O')

@app.route('/')
def index():
return render_template('index.html')
Expand All @@ -28,25 +26,27 @@ def move():

# Check if player won
if game.has_won(game.player):
return jsonify(computer_wins = False, player_wins = True, board = game.board)
return jsonify(tied = False, computer_wins = False, player_wins = True, board = game.board)
elif game.tied():
return jsonify(tied = True, computer_wins = False, player_wins = False, board = game.board)

# Calculate computer move
computer_move = game.calculate_move()

# Board is not full
if computer_move:
game.make_computer_move(computer_move['row'], computer_move['col'])
# Make the next move
game.make_computer_move(computer_move['row'], computer_move['col'])

# Check if computer won
if game.has_won(game.computer):
return jsonify(computer_row = computer_move['row'], computer_col = computer_move['col'],
computer_wins = True, player_wins = False, board = game.board)
computer_wins = True, player_wins = False, tied=False, board = game.board)
elif game.tied():
return jsonify(computer_row = computer_move['row'], computer_col = computer_move['col'],
computer_wins = False, player_wins = False, tied=True, board=game.board)

return jsonify(computer_row = computer_move['row'], computer_col = computer_move['col'],
computer_wins = False, player_wins = False, board = game.board)

if __name__ == '__main__':
app.debug = True
# app.debug = True
app.run(host='0.0.0.0')

0 comments on commit e6b8c01

Please sign in to comment.