A python game in easy way -
"i have small code of python easy implement small game,i have refered somewhere , posting there more easiest way tic tac toe game have small code of python easy implement small game,i have refered somewhere , posting there more easiest way tic tac toe game"
#import import os import time import random #define board board = ["", " ", " ", " ", " ", " ", " ", " ", " ", " "] #print header def print_header(): print""" play tic-tac-toe, need 3 in row... choices defined, must 1 9... """ #define print_board function def print_board(): print " | | " print " "+board[1]+" | "+board[2]+" | "+board[3]+" " print " | | " print "---|---|---" print " | | " print " "+board[4]+" | "+board[5]+" | "+board[6]+" " print " | | " print "---|---|---" print " | | " print " "+board[7]+" | "+board[8]+" | "+board[9]+" " print " | | " def is_winner(board, player): if (board[1] == player , board[2] == player , board[3] == player) or \ (board[4] == player , board[5] == player , board[6] == player) or \ (board[7] == player , board[8] == player , board[9] == player) or \ (board[1] == player , board[4] == player , board[7] == player) or \ (board[2] == player , board[5] == player , board[8] == player) or \ (board[3] == player , board[6] == player , board[9] == player) or \ (board[1] == player , board[5] == player , board[9] == player) or \ (board[3] == player , board[5] == player , board[7] == player): return true else: return false def is_board_full(board): if " " in board: return false else: return true while true: os.system("clear") print_header() print_board() #get player x input choice = raw_input("please choose empty space x. ") choice = int(choice) #check see if space empty first if board[choice] == " ": board[choice] = "x" else: print "sorry, space not empty!" time.sleep(1) #check x win if is_winner(board, "x"): os.system("clear") print_header() print_board() print "x wins! congratulations" break os.system("clear") print_header() print_board() #check tie (is board full) #if board full, if is_board_full(board): print "tie!" break #get player o input choice = raw_input("please choose empty space o. ") choice = int(choice) #check see if space empty first if board[choice] == " ": board[choice] = "o" else: print "sorry, space not empty!" time.sleep(1) #check o win if is_winner(board, "o"): os.system("clear") print_header() print_board() print "o wins! congratulations" break #check tie (is board full) #if board full, if is_board_full(board): print "tie!" break
Comments
Post a Comment