def test_game_win_diagonals(self): """ Tests win by diagonals """ gs = GameState() state = [1,0,0,0,1,0,0,0,1] self.assertEquals(gs.getWinner(state),1) state = [0,0,1,0,1,0,1,0,0] self.assertEquals(gs.getWinner(state),1)
def test_game_no_winner(self): """ Tests game has no winner """ gs = GameState() state = gs.reset() self.assertEquals(gs.getWinner(state),None) state = [1,1,-1,0,0,0,0,0,0] self.assertEquals(gs.getWinner(state),None)
def move(request,position,state): """ Handles player move """ gs = GameState() sGameState = str(state) iGameState = [] response_dict = {} for ci in sGameState: if int(ci) == 2: iGameState.append(board.COMPUTER_PLAYER) else: iGameState.append(int(ci)) newState = gs.move(board.HUMAN_PLAYER,iGameState,int(position)) if newState is None: response_dict.update({'status' : 'err', 'msg' : 'Invalid move'}) elif gs.isGameOver(newState): winner = gs.getWinner(newState) if winner is None: response_dict.update({'status' : 'gameover', 'msg' : 'Tie Game', 'state' : newState}) elif winner == board.HUMAN_PLAYER: response_dict.update({'status' : 'gameover', 'msg' : 'Player Wins!', 'state' : newState}) else: response_dict.update({'status' : 'gameover', 'msg' : 'Computer Wins!', 'state' : newState}) else: aiMove = botMove(newState) if aiMove is None: response_dict.update({'status' : 'err', 'msg' : 'Something has gone wrong'}) else: fState = gs.move(board.COMPUTER_PLAYER,newState,aiMove) if gs.isGameOver(fState): response_dict.update({'status' : 'gameover', 'msg' : 'Computer Wins!', 'state' : fState}) else: response_dict.update({'status' : 'ok', 'msg' : 'Your move', 'state' : fState}) return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
def new(request): """ Handles new requests for games """ gs = GameState() state = gs.reset() return render_to_response("board.html", { 'state' : state, 'info_message' : 'Your Move First'})
def test_game_not_complete_partial(self): """ Tests partially played game is not complete """ gs = GameState() state = [1,0,0,1,-1,0,-1,1,0] self.assertEquals(gs.isGameOver(state),False)
def test_game_not_complete_empty(self): """ Tests for empty game complete """ gs = GameState() state = gs.reset() self.assertEquals(gs.isGameOver(state),False)
def test_out_of_index_move(self): """ Tests for index out of range move """ gs = GameState() state = gs.reset() res = gs.move(H,state,15) self.assertEquals(res,None)
def test_invalid_move(self): """ Tests move to occupied space """ gs = GameState() state = [1,0,0,0,0,0,0,0,0] res = gs.move(H,state,0) self.assertEquals(res,None)
def test_valid_move(self): """ Tests valid move """ gs = GameState() state = gs.reset() res = gs.move(H,state,0) self.assertEquals(res[0],H)
def test_reset(self): """ Tests new game """ gs = GameState() state = gs.reset() self.assertEquals(state,[0,0,0,0,0,0,0,0,0])