I'm trying to allow the user to input the # of rows and columns that they'd like in their connect 4 game, so I'm trying to use their input to create the board. I'm trying to create a nested list so that my other code works. I've been trying for the past 2 hours, by searching the web and racking my brains for an answer. Here is the code:
import random
import ast
def winner(board):
"""This function accepts the Connect Four board as a parameter.
If there is no winner, the function will return the empty string "".
If the user has won, it will return 'X', and if the computer has
won it will return 'O'."""
for row in range(7):
count = 0
last = ''
for col in range(7):
row_win = board[row][col]
if row_win == " ":
count = 0
continue
if row_win == last:
count += 1
else:
count = 1
if count >= 4:
return row_win
last = row_win
for col in range(7):
count = 0
last = ''
for row in range(7):
col_win = board[row][col]
if col_win == " ":
count = 0
continue
if col_win == last:
count += 1
else:
count = 1
if count >= 4:
return col_win
last = col_win
for row in range(7):
for col in range(7):
try:
if (board[row][col] == board[row + 1][col + 1] and board[row + 1][col + 1] == board[row + 2][
col + 2] and
board[row + 2][col + 2] == board[row + 3][col + 3]) and (
board[row][col] != " " or board[row + 1][col + 1] != " " or board[row + 2][col + 2] != " " or
board[row + 3][col + 3] != " "):
return board[row][col]
except:
IndexError
for row in range(7):
for col in range(7):
try:
if (board[row][col] == board[row + 1][col - 1] and board[row + 1][col - 1] == board[row + 2][
col - 2] and
board[row + 2][col - 2] == board[row + 3][col - 3]) and (
board[row][col] != " " or board[row + 1][col - 1] != " " or board[row + 2][col - 2] != " " or
board[row + 3][col - 3] != " "):
return board[row][col]
except:
IndexError
# No winner: return the empty string
return ""
def display_board(board):
"""This function accepts the Connect Four board as a parameter.
It will print the Connect Four board grid (using ASCII characters)
and show the positions of any X's and O's. It also displays
the column numbers on top of the board to help
the user figure out the coordinates of their next move.
This function does not return anything."""
# print(" 1 2 3 4 5 6 7")
header = " "
i = 1
while i < len(board) + 1:
header = header + str(i) + " "
i += 1
header = header + str(i)
print(header)
for row in board:
print(" ", " | ".join(row))
print(" ---+---+---+---+---+---+---")
print()
#
# header = " "
# pipe_line = " |"
# separator = "---+"
#
# for row_num in range():
# header = header + str(row_num)
# pipe_line = pipe_line + " " + " | ".join(row))
# separator = separator + "---+"
# print()
def make_user_move(board):
"""This function accepts the Connect Four board as a parameter.
It will ask the user for a row and column. If the row and
column are each within the range of 1 and 7, and that square
is not already occupied, then it will place an 'X' in that square."""
valid_move = False
while not valid_move:
try:
col = int(input("What col would you like to move to (1-7): "))
if board[0][col - 1] != ' ':
print("Sorry, that column is full. Please try again!\n")
else:
col = col - 1
for row in range(6, -1, -1):
if board[row][col] == ' ' and not valid_move:
board[row][col] = 'X'
valid_move = True
except:
ValueError
print("Please enter a valid option! ")
return board
def make_computer_move(board):
"""This function accepts the Connect Four board as a parameter.
It will randomly pick row and column values between 0 and 6.
If that square is not already occupied it will place an 'O'
in that square. Otherwise, another random row and column
will be generated."""
computer_valid_move = False
while not computer_valid_move:
col = random.randint(0, 6)
if board[0][col] != ' ':
print("Sorry, that column is full. Please try again!\n")
else:
for row in range(6, -1, -1):
if board[row][col] == ' ' and not computer_valid_move:
board[row][col] = 'O'
computer_valid_move = True
return board
def main():
"""The Main Game Loop:"""
# free_cells = 42
# users_turn = True
# cf_board = [[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
# [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
# [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
# [" ", " ", " ", " ", " ", " ", " "]]
cf_board = []
row = int(input("How many rows do you want your game to have? "))
col = int(input("How many columns do you want your game to have? "))
free_cells = col * row
row_str = "\" \""
board_str = "\" \""
i = 0
j = 0
while i < col-1:
row_str = row_str + "," + "\" \""
i += 1
row_str = "'" + row_str + "'"
board_str = row_str + ","
while j < row-1:
board_str = board_str + row_str + ","
j += 1
Input = [board_str]
cf_board = [list(ast.literal_eval(x)) for x in Input]
# for i in range(row-1):
# cf_board.append(row_str)
while not winner(cf_board) and (free_cells > 0):
display_board(cf_board)
if users_turn:
cf_board = make_user_move(cf_board)
users_turn = not users_turn
else:
cf_board = make_computer_move(cf_board)
users_turn = not users_turn
free_cells -= 1
display_board(cf_board)
if (winner(cf_board) == 'X'):
print("Y O U W O N !")
elif (winner(cf_board) == 'O'):
print("T H E C O M P U T E R W O N !")
elif free_cells == 0:
print("S T A L E M A T E !")
print("\n*** GAME OVER ***\n")
# Start the game!
main()
I'd like it to become something formatted like this;
# cf_board = [[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
# [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
# [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
# [" ", " ", " ", " ", " ", " ", " "]]
Where the row and col depends on the user. I've tried achieving that, but I only get this huge list that's not separated by the # of rows and columns.
TL;DR
Please help me fix my code at main(), where I'm trying to construct a board with the user's inputs
Please try to ask questions only using a minimal reproducible example. If you are only trying to solve the problem of constructing a board from user input then only post the necessary code relating to that. To generate a list of lists of open spaces you could do something like this.
row = int(input("How many rows do you want your game to have? "))
col = int(input("How many columns do you want your game to have? "))
cf_board = []
rowLst = []
for i in range(row):
for j in range(col):
rowLst.append(" ")
cf_board.append(rowLst.copy())
rowLst.clear()
print(board)
for j in range(col) appends an open space to a new list. After that finishes that list is appended to the main board list and then cleared to go onto the next row.
getting that board is pretty simple. Don't need eval or anything like that.
Just do cf_board = [[" "]*col]*row to put as many space entries and lists inside as you need.
Related
Ive been learning python recently and am currently working on a small terminal game that has a character move through a maze. Im having a bug where my character is getting stuck and not allowed to move into the open spaces. Ive checked that all my movements the correct directions, and im not sure why the character is stuck.
App
played = player.Player(3, 2)
while True:
board.printBoard(played.rowPosition, played.columnPosition)
selection = input("Make a move: ")
if selection == "w":
if board.checkMove(played.rowPosition - 1, played.columnPosition):
played.moveUp()
elif selection == "s":
if board.checkMove(played.rowPosition + 1, played.columnPosition):
played.moveDown()
elif selection == "a":
if board.checkMove(played.rowPosition, played.columnPosition - 1):
played.moveLeft()
elif selection == "d":
if board.checkMove(played.rowPosition, played.columnPosition - 1):
played.moveRight()
else:
print("Invalid key")
if board.checkWin(played.rowPosition, played.columnPosition):
print("You Win!")
break;
Player:
class Player:
def __init__(self, intitalRow, initialColumn):
self.rowPosition = intitalRow
self.columnPosition = initialColumn
def moveUp(self):
self.rowPosition = self.rowPosition - 1
def moveDown(self):
self.rowPosition = self.rowPosition + 1
def moveRight(self):
self.columnPosition = self.columnPosition + 1
def moveLeft(self):
self.columnPosition = self.columnPosition - 1
Gameboard
class GameBoard:
def __init__(self):
self.winningRow = 0
self.winningColumn = 2
self.board = [
[" * ", " * ", " ", " * ", " * ", " * "],
[
" * ",
" ",
" ",
" ",
" * ",
" * ",
],
[
" * ",
" ",
" * ",
" * ",
" ",
" * ",
],
[
" * ",
" ",
" ",
" ",
" ",
" * ",
],
[" * ", " * ", " * ", " * ", " * ", " * "],
]
def printBoard(self, playerRow, playerColumn):
for i in range(len(self.board)):
for j in range(len(self.board[i])):
if i == playerRow and j == playerColumn:
print("P", end="")
else:
print(self.board[i][j], end="")
print("")
def checkMove(self, testRow, testColumn):
if self.board[testRow][testColumn].find("*") != -1:
print("Can't move there!")
return False
return True
def checkWin(self, playerRow, playerColumn):
if playerRow == 0 and playerColumn == 2:
return True
else:
return False
When the selection is "d" the same cell is being checked as when the selection is "a". I think you meant for one of these to use played.columnPosition + 1.
I am working on connect4 game, now I am on winner checking part, but the winnercheck function does not work correctly. How to fix that?
In pycharm editor it says that the variable winner is not used even it is used. And after four same digits verticaly it is not printing who is the winner. I am not sure how to fix it.
Thank you!
from termcolor import colored
field = [[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "]]
def drawField(field):
for row in range(11):
if row % 2 == 0:
rowIndex = int(row / 2)
for column in range(13):
if column % 2 == 0:
columnIndex = int(column / 2)
if column == 12:
print(field[rowIndex][columnIndex])
else:
print(field[rowIndex][columnIndex], end="")
else:
print("|", end="")
else:
print("-------------")
def reset():
for i in range(6):
for b in range(7):
field[i][b] = " "
drawField(field)
Player = 1
def winnerCheck(characters):
maxSQ = 0
char = False
sq = 0
for i in characters:
if i != char:
char = i
sq = 1
else:
sq += 1
if sq > maxSQ:
maxChar = char
maxSQ = sq
if maxChar == "X" or maxChar == "O":
if maxSQ == 4:
winner = 0
if maxChar == "X":
winner = "1"
else:
winner = "2"
print("--------------------------------------")
print("| The winner is player", winner, end="")
print(" |")
print("--------------------------------------")
while True:
rowIndex = False
currentChoice = False
print("Player turn:", Player)
column = int(input("Please enter a column: ")) - 1
if column <= 6:
print(True)
else:
print("You can choose numbers only between 1 and 7 included!")
continue
if Player == 1:
for i in range(5, -1, -1):
if field[0][column] != " ":
print("This column is already filled up! You can't put here anymore!")
full = 0
for b in range(7):
if field[0][b] != " ":
full += 1
if full == 7:
print("There is no winner!")
reset()
break
else:
if field[i][column] != " ":
continue
else:
field[i][column] = colored("X", "red")
drawField(field)
Player = 2
currentChoice = field[i][column]
rowIndex = i
break
else:
for i in range(5, -1, -1):
if field[0][column] != " ":
print("This column is already filled up! You can't put here anymore!")
full = 0
for b in range(7):
if field[0][b] != " ":
full += 1
if full == 7:
print("There is no winner!")
reset()
break
else:
if field[i][column] != " ":
continue
else:
field[i][column] = colored("O", "green")
drawField(field)
currentChoice = field[i][column]
Player = 1
rowIndex = i
break
characters = []
for i in range(6):
print(i)
characters.append(field[i][column])
print(characters)
winnerCheck(characters)
The main issue is in these lines of code:
if maxChar == "X" or maxChar == "O":
and a bit further down:
if maxChar == "X":
These conditions will never be true because your characters are never "X" or "O", but are ANSI escape codes generated by calls to colored, like '\x1b[31mX\x1b[0m'
This is a mixup between "model" and "view" aspects of your code.
The best fix is to not store the result of colored() in your field list. Instead just store plain "X" and "O" values. Then in your drawField function, do the necessary to bring color to your output.
So change:
field[i][column] = colored("X", "red")
To
field[i][column] = "X"
and make the same change for where you have colored("O", "green").
Then in drawField change:
if column == 12:
print(field[rowIndex][columnIndex])
else:
print(field[rowIndex][columnIndex], end="")
to:
ch = field[rowIndex][columnIndex]
output = colored(ch, "red" if ch == "X" else "green")
if column == 12:
print(output)
else:
print(output, end="")
Some other remarks:
Your code will only detect a vertical four in a row. Currently you only pass the information about one column to winnerCheck. You'll want to extend this function to detect also horizontal and diagonal wins...
There is unnecessary code repetition. Like the two blocks that are in the if Player == 1 .... else construct. You should try to make that just one block, as the only difference is what you assign to field[i][column].
As mentioned in comments you have a winner=0 that has no effect, since you immediately assign a different value to it. You can initialise winner in one go:
winner = "1" if maxChar == "X" else "2"
The check for a draw should not require that a user makes an invalid move. You should detect the draw when the last valid move has been made.
I'm writing Connect4 in Python. My problem is the function of player_one and player_two don't seem to be working, so the no piece is drop onto the board after player was asked to give input. I also wonder if my code to return the board after player has dropped is correct; I suspect that my present code doesn't return the board updated with a player's piece but being new, I'm not sure what to do.
Please take a look!
def field(field):
for w in range(14):
if w % 2 == 0:
usable_w = int(w/2)
for h in range(15):
if h % 2 == 0:
usable_h = int(h/2)
if h != 14:
print(field[usable_h][usable_w], end="")
else:
print(" ")
else:
print("|", end="")
else:
print("_"*13)
PlayField = [[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "]]
field(PlayField)
def player_one(field):
MoveColumn = int(input("Enter the column 1 - 7\n"))
MoveRow = 6
for row in PlayField:
if MoveColumn >= 1 and MoveColumn <= 7:
if PlayField[MoveColumn-1][MoveRow] == " ":
PlayField[MoveColumn-1][MoveRow] = "X"
break
MoveRow -= 1
return field(PlayField)
else:
print("Column outside range, please enter valid move")
def player_two(field):
MoveColumn = int(input("Enter the column 1 - 7\n"))
MoveRow = 6
for row in PlayField:
if MoveColumn >= 1 and MoveColumn <= 7:
if PlayField[MoveColumn-1][MoveRow] == " ":
PlayField[MoveColumn-1][MoveRow] = "O"
break
MoveRow -= 1
return field(PlayField)
else:
print("Column outside range, please enter valid move")
def launch_play():
while True:
Player = 1
print("Player's turn", Player)
player_one(field)
player_two(field)
launch_play()
Well, your player_... functions contain suitable statements, but in an unsuitable order; and since they operate on the global PlayField, returning it is pointless. Besides that, it's ugly having two nearly identical functions. A rearranged variant where the only difference between player one and two is passed as an argument (instead of the useless field) works as you expect:
def player(xo):
while (MoveColumn := int(input("Enter the column 1 - 7\n"))) < 1 or \
MoveColumn > 7:
print("Column outside range, please enter valid move")
MoveRow = 6
for row in PlayField:
if PlayField[MoveColumn-1][MoveRow] == " ":
PlayField[MoveColumn-1][MoveRow] = xo
field(PlayField)
break
MoveRow -= 1
In your launch_play loop you can now call
player('X')
player('O')
Now it's up to you to complete the program by checking when the game is over.
I came up with two solutions (before you modified the codes) to prevent players' turn from changing, but which couldn't work:
def player(xo):
while MoveColumn := int(input("Enter the column 1 - 7\n")):
MoveRow = 6
for row in PlayField:
if PlayField[MoveColumn-1][MoveRow] == " ":
PlayField[MoveColumn-1][MoveRow] = xo
field(PlayField)
break
Return True
MoveRow -= 1
else:
print("Column outside range, please enter valid move")
Return False
def launch_play():
while True:
Player = 'X'
print("Player's turn", Player)
player('X')
Player = '0'
print("Player's turn", Player)
player('0')
launch_play()
The other solution is to introduce player variables in the player functions (also didn't work) :
def player(xo):
while MoveColumn := int(input("Enter the column 1 - 7\n")):
MoveRow = 6
Player = 'X'
for row in PlayField:
if PlayField[MoveColumn-1][MoveRow] == " ":
PlayField[MoveColumn-1][MoveRow] = xo
field(PlayField)
break
MoveRow -= 1
Player = '0'
else:
print("Column outside range, please enter valid move")
I've completed a Connect 4 game but I want the user to be able to choose the number of rows and columns. Could somebody help me with that? Here is my code: https://paste.pythondiscord.com/iwitecuxum.py Thank you so much! The board can be found under main() and display_board(board)
def main():
"""The Main Game Loop:"""
free_cells = 42
users_turn = True
ttt_board = [[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "]]
while not winner(ttt_board) and (free_cells > 0):
display_board(ttt_board)
if users_turn:
ttt_board = make_user_move(ttt_board)
users_turn = not users_turn
else:
ttt_board = make_computer_move(ttt_board)
users_turn = not users_turn
free_cells -= 1
display_board(ttt_board)
if (winner(ttt_board) == 'X'):
print("Y O U W O N !")
elif (winner(ttt_board) == 'O'):
print("T H E C O M P U T E R W O N !")
else:
print("S T A L E M A T E !")
print("\n*** GAME OVER ***\n")
and board
def display_board(board):
"""This function accepts the Connect Four board as a parameter.
It will print the Connect Four board grid (using ASCII characters)
and show the positions of any X's and O's. It also displays
the column numbers on top of the board to help
the user figure out the coordinates of their next move.
This function does not return anything."""
print(" 0 1 2 3 4 5 6")
for row in board:
print(" " + " | ".join(row))
print("---+---+---+---+---+---+---")
print()
I'm writing a program to play Connect Four with Python and I'm not sure how to resolve the "expected an indented block" error that is being thrown at me in certain areas. Any time I try running a [row][col] section, it throws the error up. I'm using some example code to help me develop my own and here's what I'm using:
import random
def winner(board):
"""This function accepts the Connect 4 board as a parameter.
If there is no winner, the function will return the empty string "".
If the user has won, it will return 'X', and if the computer has
won it will return 'O'."""
# Check rows for winner
for row in range(6):
for col in range(3):
if (board[row][col] == board[row][col + 1] == board[row][col + 2] ==\
board[row][col + 3]) and (board[row][col] != " "):
return board[row][col]
# Check columns for winner
for col in range(6):
for row in range(3):
if (board[row][col] == board[row + 1][col] == board[row + 2][col] ==\
board[row + 3][col]) and (board[row][col] != " "):
return board[row][col]
# Check diagonal (top-left to bottom-right) for winner
for row in range(3):
for col in range(4):
if (board[row][col] == board[row + 1][col + 1] == board[row + 2][col + 2] ==\
board[row + 3][col + 3]) and (board[row][col] != " "):
return board[row][col]
# Check diagonal (bottom-left to top-right) for winner
for row in range(5, 2, -1):
for col in range(3):
if (board[row][col] == board[row - 1][col + 1] == board[row - 2][col + 2] ==\
board[row - 3][col + 3]) and (board[row][col] != " "):
return board[row][col]
# No winner: return the empty string
return ""
def display_board(board):
print " 1 2 3 4 5 6 7"
print "1: " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " | " + board[0][3] + " | " + board[0][4] + " | " + board[0][5] + " | " + board[0][6] + " | " + board[0][7]
print " ---+---+---+---+---+---+---"
print "2: " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " | " + board[1][3] + " | " + board[1][4] + " | " + board[1][5] + " | " + board [1][6] + " | " + board [1][7]
print " ---+---+---+---+---+---+---+"
print "3: " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " | " + board[2][3] + " | " + board [2][4] + " | " + board [2][5] + " | " + board [2][6] + " | " + board [2][7]
print " ---+---+---+---+---+---+---+"
print "4: " + board[3][0] + " | " + board[3][1] + " | " + board[3][2] + " | " + board[3][3] + " | " + board [3][4] + " | " + board [3][5] + " | " + board [3][6] + " | " + board [3][7]
print " ---+---+---+---+---+---+---+"
print "5: " + board[4][0] + " | " + board[4][1] + " | " + board[4][2] + " | " + board[4][3] + " | " + board [4][4] + " | " + board [4][5] + " | " + board [4][6] + " | " + board [4][7]
print " ---+---+---+---+---+---+---+"
print "6: " + board[5][0] + " | " + board[5][1] + " | " + board[5][2] + " | " + board[5][3] + " | " + board [5][4] + " | " + board [5][5] + " | " + board [5][6] + " | " + board [5][7]
print
def make_user_move(board):
try:
valid_move = False
while not valid_move:
col = input("What col would you like to move to (1-7):")
for row in range (6,0,-1):
if (1 <= row <= 6) and (1 <= col <= 7) and (board[row-1][col-1] == " "):
board[row-1][col-1] = 'X'
valid_move = True
break
else:
print "Sorry, invalid square. Please try again!\n"
except NameError:
print "Only numbers are allowed."
except IndexError:
print "You can only select columns from (1-7), and rows from (1-6)."
def make_computer_move(board):
# Code needed here...
valid_move = False
while not valid_move:
row = random.randint(0,5)
col = random.randint(0, 6)
for row in range (5,0,-1):
if board[row][col] == " ":
board[row][col] = "O"
valid_move = True
break
def main():
free_cells = 42
users_turn = True
count = 1
ttt_board = [ [ " ", " ", " ", " ", " ", " "," ", " "], [ " ", " ", " ", " ", " "," ", " ", " "], [ " ", " ", " ", " ", " ", " ", " ", " "], [ " ", " ", " ", " ", " ", " ", " ", " "], [ " ", " ", " ", " ", " ", " ", " ", " "], [ " ", " ", " ", " ", " ", " ", " ", " "] ]
print "\nHALL OF FAME \n"
try:
hall_of_fame = open("HallOfFame.txt", 'r')
for name in hall_of_fame:
print str(count) + ".", name
print
count += 1
hall_of_fame.close()
except IOError:
print "No Human Has Ever Beat Me.. mwah-ha-ha-ha!\n"
choice = raw_input("Would you like to go first? (y or n): ")
if (choice == 'y' or choice=='Y'):
users_turn = True
elif (choice == 'n' or choice =='N') :
users_turn = False
else:
print 'invalid input'
while not winner(ttt_board) and (free_cells > 0):
display_board(ttt_board)
if users_turn:
make_user_move(ttt_board)
users_turn = not users_turn
else:
make_computer_move(ttt_board)
users_turn = not users_turn
free_cells -= 1
display_board(ttt_board)
if (winner(ttt_board) == 'X'):
print "You Won!"
print "Your name will now be added to the Hall of Fame!"
hall_of_fame = open("HallOfFame.txt", 'a')
name = raw_input("Enter your name: ")
hall_of_fame.write(name+ '\n')
print "Your name has been added to the Hall of Fame!"
hall_of_fame.close()
print "\nGAME OVER"
elif (winner(ttt_board) == 'O'):
print "The Computer Won!"
print "\nGAME OVER"
else:
print "Stalemate!"
print "\nGAME OVER \n"
#start the game
main()
It looks like you are not indenting your for loops:
for row in range(6):
for col in range(3):
if (board[row][col] == board[row][col + 1] == board[row][col + 2] ==\
board[row][col + 3]) and (board[row][col] != " "):
return board[row][col]
The : denotes the need for an indent on the next line.
There are a LOT of places where the indentation is just plain missing for inexplicable reasons. As #davidism mentions, indentation in python is used to indicate nesting levels; so this code is completely unusable in its current state.
Here's my attempt to GUESS what your code was MEANT to do, so you can get an idea of all the places where whitespace matters, but it is just missing in your code.
import random
def winner(board):
for row in range(6):
for col in range(3):
if (board[row][col] == board[row][col + 1] == board[row][col + 2] == \
board[row][col + 3]) and (board[row][col] != " "):
return board[row][col] # Check columns for winner
for col in range(6):
for row in range(3):
if (board[row][col] == board[row + 1][col] == board[row + 2][col] == \
board[row + 3][col]) and (board[row][col] != " "):
return board[row][col]
# Check diagonal (top-left to bottom-right) for winner
for row in range(3):
for col in range(4):
if (board[row][col] == board[row + 1][col + 1] == board[row + 2][col + 2] == \
board[row + 3][col + 3]) and (board[row][col] != " "):
return board[row][col]
# Check diagonal (bottom-left to top-right) for winner
for row in range(5, 2, -1):
for col in range(3):
if (board[row][col] == board[row - 1][col + 1] == board[row - 2][col + 2] == \
board[row - 3][col + 3]) and (board[row][col] != " "):
return board[row][col]
# No winner: return the empty string
return ""
def display_board(board):
print " 1 2 3 4 5 6 7"
print "1: " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " | " + board[0][3] + " | " + board[0][
4] + " | " + board[0][5] + " | " + board[0][6] + " | " + board[0][7]
print " ---+---+---+---+---+---+---"
print "2: " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " | " + board[1][3] + " | " + board[1][
4] + " | " + board[1][5] + " | " + board[1][6] + " | " + board[1][7]
print " ---+---+---+---+---+---+---+"
print "3: " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " | " + board[2][3] + " | " + board[2][
4] + " | " + board[2][5] + " | " + board[2][6] + " | " + board[2][7]
print " ---+---+---+---+---+---+---+"
print "4: " + board[3][0] + " | " + board[3][1] + " | " + board[3][2] + " | " + board[3][3] + " | " + board[3][
4] + " | " + board[3][5] + " | " + board[3][6] + " | " + board[3][7]
print " ---+---+---+---+---+---+---+"
print "5: " + board[4][0] + " | " + board[4][1] + " | " + board[4][2] + " | " + board[4][3] + " | " + board[4][
4] + " | " + board[4][5] + " | " + board[4][6] + " | " + board[4][7]
print " ---+---+---+---+---+---+---+"
print "6: " + board[5][0] + " | " + board[5][1] + " | " + board[5][2] + " | " + board[5][3] + " | " + board[5][
4] + " | " + board[5][5] + " | " + board[5][6] + " | " + board[5][7]
print
def make_user_move(board):
try:
valid_move = False
while not valid_move:
col = input("What col would you like to move to (1-7):")
for row in range(6, 0, -1):
if (1 <= row <= 6) and (1 <= col <= 7) and (board[row - 1][col - 1] == " "):
board[row - 1][col - 1] = 'X'
valid_move = True
break
else:
print "Sorry, invalid square. Please try again!\n"
except NameError:
print "Only numbers are allowed."
except IndexError:
print "You can only select columns from (1-7), and rows from (1-6)."
def make_computer_move(board):
# Code needed here...
valid_move = False
while not valid_move:
row = random.randint(0, 5)
col = random.randint(0, 6)
for row in range(5, 0, -1):
if board[row][col] == " ":
board[row][col] = "O"
valid_move = True
break
def main():
free_cells = 42
users_turn = True
count = 1
ttt_board = [[" ", " ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " ", " "]]
print "\nHALL OF FAME \n"
try:
hall_of_fame = open("HallOfFame.txt", 'r')
for name in hall_of_fame:
print str(count) + ".", name
print
count += 1
hall_of_fame.close()
except IOError:
print "No Human Has Ever Beat Me.. mwah-ha-ha-ha!\n"
choice = raw_input("Would you like to go first? (y or n): ")
if (choice == 'y' or choice == 'Y'):
users_turn = True
elif (choice == 'n' or choice == 'N'):
users_turn = False
else:
print 'invalid input'
while not winner(ttt_board) and (free_cells > 0):
display_board(ttt_board)
if users_turn:
make_user_move(ttt_board)
users_turn = not users_turn
else:
make_computer_move(ttt_board)
users_turn = not users_turn
free_cells -= 1
display_board(ttt_board)
if (winner(ttt_board) == 'X'):
print "You Won!"
print "Your name will now be added to the Hall of Fame!"
hall_of_fame = open("HallOfFame.txt", 'a')
name = raw_input("Enter your name: ")
hall_of_fame.write(name + '\n')
print "Your name has been added to the Hall of Fame!"
hall_of_fame.close()
print "\nGAME OVER"
elif (winner(ttt_board) == 'O'):
print "The Computer Won!"
print "\nGAME OVER"
else:
print "Stalemate!"
print "\nGAME OVER \n"
# start the game
main()