Reputation: 19
Hi i am new to python and i am creating a connect 4 game, i am on the final bit, the play function which allows the game to be played. The issue appears to be my while loop, it runs once, ask the user for a column and inputs the counter in that column, however after that it just keeps on asking which column to select and not changing the board:
who = game['who']
while who != 'computer':
x = int(input("Which column to select? "))
game['board'] = board2
l = getValidMoves(game['board'])
if x in l:
game['board'] = makeMove(board2, x, who)
printBoard(game['board'])
board = game['board']
if hasWon(game['board'], who) == True:
print("{who} has won.")
sys.exit()
You can see when i run this code i get:
Which column to select? 1
|1|2|3|4|5|6|7|
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| |X| | | | | |
Which column to select? 2
Which column to select?
I think the issue is that the while loop is stopping running after asking for what column to select. Any help would be appreciated.
My printBoard function is:
print("|1|2|3|4|5|6|7|")
print("---------------")
for j in range(6):
for i in range(7):
if board[j][i] == 1:
board[j][i] = "X"
elif board[j][i] == 2:
board[j][i] = "O"
elif board[j][i] == 0:
board[j][i] = " "
for j in range(6):
print("|"+"|".join(str(board[j][i]) for i in range(7))+"|")
return None
My getValidMoves is:
l = list()
for i in range(7):
if board[0][i] == 0:
l.append(i)
return l
enter code here
and my makeMove is:
if who == 1:
for i in [5,4,3,2,1,0]:
if board[i][move] == 0:
break
board[i][move] = 1
elif who == 2:
for i in [5,4,3,2,1,0]:
if board[i][move] == 0:
break
board[i][move] = 2
return board
Upvotes: 1
Views: 273
Reputation: 599628
Your loop continues as long as who
does not equal "computer". But nothing inside the loop ever changes who
, so it will never equal "computer" and the loop will never complete.
Upvotes: 2