I'm trying to get more coding experience and decided to start small with a basic noughts & crosses game.
Any feedback is welcome and below are the items I think I should add
- a print out of an actual grid not the array
- more error catching ie user enters T instead of X or O
- potential position recommendations
- more comments in the code
import random
def grid(n):
board = []
for i in range(n):
board.append("-")
return board
board1 = grid(9)
print(board1)
p1 = input("Enter your value X or O: ")
if p1 =='X':
p2 = 'O'
else:
p2 = 'X'
def check_column(board, p, pos):
if board[pos + 3*0] == p:
if board[pos+3*1] == p:
if board[pos+3*2] == p:
return True
def check_row(board, p, pos):
if board[pos + 0] == p:
if board[pos + 1] == p:
if board[pos + 2] == p:
return True
def check_dig1(board, p, pos):
if board[pos + 0] == p:
if board[pos + 2] == p:
if board[pos + 4] == p:
return True
def check_dig2(board, p, pos):
if board[pos + 0] == p:
if board[pos + 4] == p:
if board[pos + 8] == p:
return True
def check_win(board, player):
length = len(board)
#column check
for i in range(3):
if check_column(board, player,i):
print('col')
return True
for i in range(0,7,3):
if check_row(board, player,i):
print('col')
return True
if check_dig1(board, player, 2):
print('diag1')
return True
if check_dig2(board, player, 0):
print('diag2')
return True
return False
while True:
#val, pos = input("Enter two values: ").split(',')
position = input("P1 enter your position: ")
board1[int(position)-1] = p1
print(board1)
if check_win(board1,p1):
print('P1 has won')
print(board1)
break
if board1.count('X') + board1.count('O') == 9:
print("Tie!")
break
position = input("P2 enter your position: ")
board1[int(position)-1] = p2
print(board1)
if check_win(board1,p2):
print('P2 has won')
print(board1)
break
if board1.count('X') + board1.count('O') == 9:
print("Tie!")
break
# check_win(board1, 'O')
# print(board1)