print("# Matthew's Noughts and Crosses Simulator 2014 #") X = "X" O = "O" victory = "No" playingSurface = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]] def endGame(): global victory victory = "yes" def displayGrid(grid): print(grid[0][0], "|", grid[0][1], "|", grid[0][2]) print("---------") print(grid[1][0], "|", grid[1][1], "|", grid[1][2]) print("---------") print(grid[2][0], "|", grid[2][1], "|", grid[2][2]) displayGrid(playingSurface) def moveX(xsmove): if xsmove == "1" and playingSurface[0][0] == "1": playingSurface[0][0] = X elif xsmove == "1" and playingSurface[0][0] != "1": print("You can't go there!") elif xsmove == "2" and playingSurface[0][1] == "2": playingSurface[0][1] = X elif xsmove == "2" and playingSurface[0][1] != "2": print("You can't go there!") elif xsmove == "3" and playingSurface[0][2] == "3": playingSurface[0][2] = X elif xsmove == "3" and playingSurface[0][2] != "3": print("You can't go there!") elif xsmove == "4" and playingSurface[1][0] == "4": playingSurface[1][0] = X elif xsmove == "4" and playingSurface[1][0] != "4": print("You can't go there!") elif xsmove == "5" and playingSurface[1][1] == "5": playingSurface[1][1] = X elif xsmove == "5" and playingSurface[1][1] != "5": print("You can't go there!") elif xsmove == "6" and playingSurface[1][2] == "6": playingSurface[1][2] = X elif xsmove == "6" and playingSurface[1][2] != "6": print("You can't go there!") elif xsmove == "7" and playingSurface[2][0] == "7": playingSurface[2][0] = X elif xsmove == "7" and playingSurface[2][0] != "7": print("You can't go there!") elif xsmove == "8" and playingSurface[2][1] == "8": playingSurface[2][1] = X elif xsmove == "8" and playingSurface[2][1] != "8": print("You can't go there!") elif xsmove == "9" and playingSurface[2][2] == "9": playingSurface[2][2] = X elif xsmove == "9" and playingSurface[2][2] != "9": print("You can't go there!") else: print("Move not valid (1-9)") displayGrid(playingSurface) checkWin(playingSurface) return (xsmove) def moveO(osmove): if osmove == "1" and playingSurface[0][0] == "1": playingSurface[0][0] = O elif osmove == "1" and playingSurface[0][0] != "1": print("You can't go there!") elif osmove == "2" and playingSurface[0][1] == "2": playingSurface[0][1] = O elif osmove == "2" and playingSurface[0][1] != "2": print("You can't go there!") elif osmove == "3" and playingSurface[0][2] == "3": playingSurface[0][2] = O elif osmove == "3" and playingSurface[0][2] != "3": print("You can't go there!") elif osmove == "4" and playingSurface[1][0] == "4": playingSurface[1][0] = O elif osmove == "4" and playingSurface[1][0] != "4": print("You can't go there!") elif osmove == "5" and playingSurface[1][1] == "5": playingSurface[1][1] = O elif osmove == "5" and playingSurface[1][1] != "5": print("You can't go there!") elif osmove == "6" and playingSurface[1][2] == "6": playingSurface[1][2] = O elif osmove == "6" and playingSurface[1][2] != "6": print("You can't go there!") elif osmove == "7" and playingSurface[2][0] == "7": playingSurface[2][0] = O elif osmove == "7" and playingSurface[2][0] != "7": print("You can't go there!") elif osmove == "8" and playingSurface[2][1] == "8": playingSurface[2][1] = O elif osmove == "8" and playingSurface[2][1] != "8": print("You can't go there!") elif osmove == "9" and playingSurface[2][2] == "9": playingSurface[2][2] = O elif osmove == "9" and playingSurface[2][2] != "9": print("You can't go there!") else: print("Move not valid (1-9)") displayGrid(playingSurface) checkWin(playingSurface) return (osmove) def checkWin(playingSurface): if playingSurface[0][0] == X and playingSurface[0][1] == X and playingSurface[0][2] == X: print("P1 (X) is victorious!") endGame() elif playingSurface[0][0] == O and playingSurface[0][1] == O and playingSurface[0][2] == O: print("P2 (O) is victorious!") endGame() elif playingSurface[0][0] == X and playingSurface[1][0] == X and playingSurface[2][0] == X: print("P1 (X) is victorious!") endGame() elif playingSurface[0][0] == O and playingSurface[1][0] == O and playingSurface[2][0] == O: print("P2 (O) is victorious!") endGame() elif playingSurface[1][0] == X and playingSurface[1][1] == X and playingSurface[1][2] == X: print("P1 (X) is victorious!") endGame() elif playingSurface[1][0] == O and playingSurface[1][1] == O and playingSurface[1][2] == O: print("P2 (O) is victorious!") gendGame() elif playingSurface[2][0] == X and playingSurface[2][1] == X and playingSurface[2][2] == X: print("P1 (X) is victorious!") endGame() elif playingSurface[2][0] == O and playingSurface[2][1] == O and playingSurface[2][2] == O: print("P2 (O) is victorious!") endGame() elif playingSurface[0][1] == X and playingSurface[1][1] == X and playingSurface[2][1] == X: print("P1 (X) is victorious!") endGame() elif playingSurface[0][1] == O and playingSurface[1][1] == O and playingSurface[2][1] == O: print("P2 (O) is victorious!") endGame() elif playingSurface[0][2] == X and playingSurface[1][2] == X and playingSurface[2][2] == X: print("P1 (X) is victorious!") endGame() elif playingSurface[0][2] == O and playingSurface[1][2] == O and playingSurface[2][2] == O: print("P2 (O) is victorious!") endGame() elif playingSurface[0][0] == X and playingSurface[1][1] == X and playingSurface[2][2] == X: print("P1 (X) is victorious!") endGame() elif playingSurface[0][0] == O and playingSurface[1][1] == O and playingSurface[2][2] == O: print("P2 (O) is victorious!") endGame() elif playingSurface[0][2] == X and playingSurface[1][1] == X and playingSurface[2][0] == X: print("P1 (X) is victorious!") endGame() elif playingSurface[0][2] == O and playingSurface[1][1] == O and playingSurface[2][0] == O: print("P2 (O) is victorious!") endGame() elif (playingSurface[0][0] != "1") and (playingSurface[0][1] != "2") and (playingSurface[0][2] != "3") and (playingSurface[1][0] != "4") and (playingSurface[1][1] != "5") and (playingSurface[1][2] != "6") and (playingSurface[2][0] != "7") and (playingSurface[2][1] != "8") and (playingSurface[2][2] != "9"): print("Tied Game") else: return playingSurface while victory == "No": moveX1 = input ("Player 1's first move (X). Input a number.") moveX(moveX1) moveO1 = input ("Player 2's first move (O). Input a number.") moveO(moveO1) print("End of Game")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question