0

This is a very peculiar problem I am having. This code (below) raises no error, but doesn't update and show the move, by player or computer.
What is the the problem?? I am really confused.
Also, All functions that are not defined in this code are imported from TicTacToe and Work!
There is no problem with the game logic functions, the problem is with the display(pygame) code.
Inputs are currently from console

from TicTacToe import * # The game logic functions
import pygame
from pygame.locals import *
#-------------The problem may be here---------------#
def printb(board,surface,image): 
    surface.blit(image,(0,0))
    for x in range(3):
        for y in range(3):
            disp(board[x+(y*3)],(58*x,58*y+13),surface)  # any inputs on a better way to do this??
#-------------The problem may be here---------------#
def disp(phrase,loc,screen):
    s = font.render(phrase, True, (0,0,0))
    screen.blit(s, loc)

if __name__ == '__main__':
    # Pygame
    pygame.init()
    pygame.font.init()

    screen = pygame.display.set_mode((200,250))
    screen.fill((0,255,0))

    img = pygame.image.load('Board.png')
    img = img.convert_alpha()

    font = pygame.font.SysFont("Courier",11)
#----------The problem may even be here---------------#
    # Game
    board = [' ']*9
    playing = True
    while True:
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()

            printb(board,screen,img)

            pmove = playermove(board) # player move
            makemove(board,pmove,'O')

            if checkwinner(board,'O'):
                winner = 'P'
                break

            else:
                if boardfull(board):
                    break
                cmove = compmove(board) # comp move
                makemove(board,cmove,'X')

                if checkwinner(board,'X'):
                    winner = 'C'
                    break

            pygame.display.flip()
        printb(board,screen,img)
        if winner == 'P':
            print 'You'
        elif winner == 'C':
            print 'I'
        else:
            print'Draw'
        if playagain():
            board = [' ']*9
            print '\n\n'
        else:
            break

In one of the runs(On IDLE), player is ' O ', computer is ' X '

>>> 
Enter Your Move: 1
Enter Your Move: 2
Enter Your Move: 3
Not free space!!
Enter Your Move: 4
I Win !!
Play Again? -> n
>>> board
['O', 'O', 'X', 'O', ' ', 'X', ' ', ' ', 'X']
>>> winner
'C'

Here's the pygame window, It stays the same at all times.
The Window

pradyunsg
  • 16,339
  • 10
  • 39
  • 91
  • Have you inserted print statements to see where the code hangs, perhaps? – Martijn Pieters Jan 15 '13 at 15:05
  • @MartijnPieters Yes, It runs the printb() before the player input (in the game loop) and and in each loop, the problem does seem to be in the printb() func. And no the code doesn't hang anywhere. – pradyunsg Jan 15 '13 at 15:10
  • 1
    Well, one thing, `board[x+y]` isn't going to get you what you want. You want `board[x + y * 3]`. `x+y` will get the same element for [0,3] as [3,0]. `x + y * 3` will get you the xth element offset by the yth line. – Silas Ray Jan 15 '13 at 15:13
  • @sr2222 Thx for that, changed code both in my script and in the question. – pradyunsg Jan 15 '13 at 15:22
  • Admittedly guessing here, but what happens if you do `s.convert_alpha()` before bliting it to the screen buffer? – Silas Ray Jan 15 '13 at 17:33
  • 1
    you should take small steps to see, where the problem lays. Try printing out text on the bottom of the window. Maybe there is something wrong with the font? – Bartlomiej Lewandowski Jan 15 '13 at 22:33
  • Wait a second, what do `playermove()` and `makemove()` do? They don't by any chance attempt to take input from stdin, do they? If so, how are you attempting to write to stdin when running the pygame code? My guess is that you are never actually taking any user input and thus never actually updating the board. – Silas Ray Jan 15 '13 at 22:44
  • @sr2222 No, as i said the logic methods works perfect, there is no problem with either `playermove()` or `makemove()`. `playermove()` returns what move the player has entered with other errors like space already taken care of (look in the run part) like invalid inputs. `makemove()` is for changing the item on the board, that is making the move on the list. – pradyunsg Jan 16 '13 at 08:30
  • So how do you enter moves when running in pygame? Just because it works fine in IDLE or from the interactive interpreter doesn't mean it works fine when operating from a gui. – Silas Ray Jan 16 '13 at 11:12
  • @sr2222 Currently I am not taking input from Pygame, it is from the console. – pradyunsg Jan 16 '13 at 11:18
  • Are you drawing the text in white? – Blckknght Jan 16 '13 at 12:00
  • @Blckknght No, in (0,0,0) black. See `disp()` – pradyunsg Jan 16 '13 at 12:01

0 Answers0