Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #The sys module allows an exit without a load of bad red writing!
- import sys
- cross = "X"
- nought = "O"
- blank = "#"
- space = " "
- def createGrid(size):
- #Creates a 2d array, then fills it with the correct number of rows and columns
- #A size of N gives an NxN grid. You could adapt this program to have different sized boards this way.
- global grid
- grid = [[blank]]
- #Add sufficient columns:
- for i in range(1,size):
- grid.append([blank])
- #Add sufficient rows:
- for j in range(1,size+1):
- for k in range(1,size):
- grid[j-1].append(blank)
- #You can referance the contents of a square using format grid[row][column]. Bear in mind 0 is the first row, not 1!
- def changeGrid(row,column,new):
- if row == "1":
- rowActual = 0
- elif row == "2":
- rowActual = 1
- elif row == "3":
- rowActual = 2
- else:
- print("Sorry, your row was invalid. I'm going to crash now. ZZZZzzzzz....")
- sys.exit()
- print("Row actual is",rowActual)
- if column == "a":
- columnActual = 0
- elif column == "b":
- columnActual = 1
- elif column == "c":
- columnActual = 2
- else:
- print("Sorry, your column was invalid. I'm going to crash now. ZZZZzzzzz....")
- sys.exit()
- print("Column Actual is",columnActual)
- global grid
- if (grid[rowActual][columnActual] == cross and new == nought) or (grid[rowActual][columnActual] == nought and new == cross):
- print("Sorry, that space is taken! I don't know what to do now so I'm gonna go to sleep. ZZZZZzzzzz....")
- sys.exit()
- grid[rowActual][columnActual] = new
- print(grid[rowActual][columnActual])
- printGrid()
- def printGrid():
- print(" A B C")
- print("")
- print("1 ",grid[0][0],space,grid[0][1],space,grid[0][2])
- print("")
- print("2 ",grid[1][0],space,grid[1][1],space,grid[1][2])
- print("")
- print("3 ",grid[2][0],space,grid[2][1],space,grid[2][2])
- def winMessage(player):
- print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
- print(player, "has won the game!")
- print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
- sys.exit()
- def checkWin(player):
- win = False
- if grid[0][0] == grid[0][1] == grid[0][2] != blank:
- win = True
- elif grid[1][0] == grid[1][1] == grid[1][2] != blank:
- win = True
- elif grid[2][0] == grid[2][2] == grid[2][2] != blank:
- win = True
- elif grid[0][0] == grid[1][0] == grid[2][0] != blank:
- win = True
- elif grid[0][1] == grid[1][1] == grid[2][1] != blank:
- win = True
- elif grid[0][2] == grid[1][2] == grid[2][2] != blank:
- win = True
- elif grid[0][0] == grid[1][1] == grid[2][2] != blank:
- win = True
- elif grid[0][2] == grid[1][1] == grid[2][0] != blank:
- win = True
- if win == True:
- winMessage(player)
- def takeTurn(player):
- if player == cross:
- print("******************************************")
- print("It's X's turn!")
- print("******************************************")
- if player == nought:
- print("******************************************")
- print("It's O's turn!")
- print("******************************************")
- printGrid()
- row = input("What row do you want to play (1-3):")
- column = input("What column do you want to play (A-C):")
- changeGrid(row,column,player)
- checkWin(player)
- #Main body of the code:
- def main():
- createGrid(3)
- #Limited to 100 cycles for security purposes
- for i in range(1,100):
- takeTurn(cross)
- takeTurn(nought)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement