Advertisement
JonneOpettaja

TicTacToeVersioC

Feb 28th, 2023
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. import os
  2.  
  3. os.system("cls")
  4.  
  5. board = [" ", " ", " ", " ", " ", " "," ", " ", " ", " "]
  6. space1 = "                    " #20 merkkiä
  7. space2 = "          " #10 merkkiä
  8. space3 = "   " #3 merkkiä
  9.  
  10. def display_game(board):
  11.     print("Tässä tilanne" + space2 + "Tässä ohje valintaan")
  12.     print(space3 + board[7] + "|" + board[8] + "|" + board[9] + space1 + "7|8|9")
  13.     print(space3 + board[4] + "|" + board[5] + "|" + board[6] + space1 + "4|5|6")
  14.     print(space3 + board[1] + "|" + board[2] + "|" + board[3] + space1 + "1|2|3")
  15.     #check_winner()
  16.  
  17. def sijainnin_valinta(board):
  18.     choice = "Wrong"
  19.     while choice not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
  20.         choice = input("Valitse sijainti (1,2,3,4,5,6,7,8,9): ")
  21.         if choice not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
  22.             print("Väärä valinta!")
  23.         elif board[int(choice)] == "X" or board[int(choice)] == "O":
  24.             choice = "Wrong"
  25.             print("Varattu ruutu!")
  26.     return int(choice)
  27.  
  28. def valinnan_muutos(board, sijainti, phase):
  29.     if phase%2 == 0:
  30.         board[sijainti] = "O"
  31.     else:
  32.         board[sijainti] = "X"
  33.     return board
  34.  
  35. def gameon_choice():
  36.     choice = "Wrong"
  37.     while choice not in ["K", "E"]:
  38.         choice = input("Haluatko jatkaa pelaamista K/E): ")
  39.         if choice not in ["K", "E"]:
  40.             print("Väärä valinta!")
  41.     if choice == "K":
  42.         return True
  43.     else:
  44.         return False
  45.  
  46. def pelimoottori(board):
  47.     game_on = True #peli jatkuu niin kauan kuin tämä on True
  48.     game_won = " "
  49.     phase = 1 #pariton tarkoittaa pelaajan 1 vuoroa, parillinen pelaajan 2 vuoroa
  50.     while game_on: #peli-silmukka, joka jatkuu kunnes game_on saa arvon False
  51.         os.system("cls")
  52.         display_game(board)
  53.         sijainti = sijainnin_valinta(board)
  54.         board = valinnan_muutos(board, sijainti, phase)
  55.         phase += 1
  56.         os.system("cls")
  57.         display_game(board)
  58.         game_won = check_winner()
  59.         if game_won != " ":
  60.             win(game_won)
  61.             game_on = False
  62.         if phase == 10:
  63.             print("tasuri!")
  64.             game_on = gameon_choice()
  65.             if game_on == True:
  66.                 board = nollaa_board()
  67.             else:
  68.                 return
  69.  
  70. def check_winner():
  71.     winner = " "
  72.     if board[7] == board[8] == board[9] == "X":
  73.         winner == "X"
  74.     if board[7] == board[8] == board[9] == "O":
  75.         winner == "O"
  76.     if board[5] == "X":
  77.         winner == "X"
  78.     return winner
  79.  
  80. def win(game_won):
  81.     input("jee")
  82.     print(f"Game won by player {game_won}!!!")
  83.  
  84. def nollaa_board():
  85.     return [" ", " ", " ", " ", " ", " "," ", " ", " ", " "]
  86.  
  87. pelimoottori(board)
  88.  
  89. game_on = gameon_choice()
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement