Advertisement
Guest User

Untitled

a guest
Jul 6th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #The sys module allows an exit without a load of bad red writing!
  2. import sys
  3.  
  4. cross = "X"
  5. nought = "O"
  6. blank = "#"
  7. space = " "
  8.  
  9. def createGrid(size):
  10. #Creates a 2d array, then fills it with the correct number of rows and columns
  11. #A size of N gives an NxN grid. You could adapt this program to have different sized boards this way.
  12. global grid
  13. grid = [[blank]]
  14. #Add sufficient columns:
  15. for i in range(1,size):
  16. grid.append([blank])
  17. #Add sufficient rows:
  18. for j in range(1,size+1):
  19. for k in range(1,size):
  20. grid[j-1].append(blank)
  21. #You can referance the contents of a square using format grid[row][column]. Bear in mind 0 is the first row, not 1!
  22.  
  23. def changeGrid(row,column,new):
  24. if row == "1":
  25. rowActual = 0
  26. elif row == "2":
  27. rowActual = 1
  28. elif row == "3":
  29. rowActual = 2
  30. else:
  31. print("Sorry, your row was invalid. I'm going to crash now. ZZZZzzzzz....")
  32. sys.exit()
  33. print("Row actual is",rowActual)
  34. if column == "a":
  35. columnActual = 0
  36. elif column == "b":
  37. columnActual = 1
  38. elif column == "c":
  39. columnActual = 2
  40. else:
  41. print("Sorry, your column was invalid. I'm going to crash now. ZZZZzzzzz....")
  42. sys.exit()
  43. print("Column Actual is",columnActual)
  44. global grid
  45. if (grid[rowActual][columnActual] == cross and new == nought) or (grid[rowActual][columnActual] == nought and new == cross):
  46. print("Sorry, that space is taken! I don't know what to do now so I'm gonna go to sleep. ZZZZZzzzzz....")
  47. sys.exit()
  48. grid[rowActual][columnActual] = new
  49. print(grid[rowActual][columnActual])
  50. printGrid()
  51.  
  52. def printGrid():
  53. print(" A B C")
  54. print("")
  55. print("1 ",grid[0][0],space,grid[0][1],space,grid[0][2])
  56. print("")
  57. print("2 ",grid[1][0],space,grid[1][1],space,grid[1][2])
  58. print("")
  59. print("3 ",grid[2][0],space,grid[2][1],space,grid[2][2])
  60.  
  61. def winMessage(player):
  62. print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  63. print(player, "has won the game!")
  64. print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  65. sys.exit()
  66.  
  67. def checkWin(player):
  68. win = False
  69. if grid[0][0] == grid[0][1] == grid[0][2] != blank:
  70. win = True
  71. elif grid[1][0] == grid[1][1] == grid[1][2] != blank:
  72. win = True
  73. elif grid[2][0] == grid[2][2] == grid[2][2] != blank:
  74. win = True
  75. elif grid[0][0] == grid[1][0] == grid[2][0] != blank:
  76. win = True
  77. elif grid[0][1] == grid[1][1] == grid[2][1] != blank:
  78. win = True
  79. elif grid[0][2] == grid[1][2] == grid[2][2] != blank:
  80. win = True
  81. elif grid[0][0] == grid[1][1] == grid[2][2] != blank:
  82. win = True
  83. elif grid[0][2] == grid[1][1] == grid[2][0] != blank:
  84. win = True
  85. if win == True:
  86. winMessage(player)
  87.  
  88. def takeTurn(player):
  89. if player == cross:
  90. print("******************************************")
  91. print("It's X's turn!")
  92. print("******************************************")
  93. if player == nought:
  94. print("******************************************")
  95. print("It's O's turn!")
  96. print("******************************************")
  97. printGrid()
  98. row = input("What row do you want to play (1-3):")
  99. column = input("What column do you want to play (A-C):")
  100. changeGrid(row,column,player)
  101. checkWin(player)
  102.  
  103. #Main body of the code:
  104. def main():
  105. createGrid(3)
  106. #Limited to 100 cycles for security purposes
  107. for i in range(1,100):
  108. takeTurn(cross)
  109. takeTurn(nought)
  110.  
  111. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement