This forum is in archive mode. You will not be able to post new content.

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Bugogy

Pages: [1]
1
Scripting Languages / Re: [Python] Tic Tac Toe
« on: June 04, 2013, 10:25:33 PM »
Thank you for the response! I'll look into the issues you addressed, try to clean up the repetition, and reduce the usage of global variables. I highly appreciate your feedback and will take this opportunity to learn from my mistakes.

2
Scripting Languages / Re: [Python] Tic Tac Toe
« on: May 23, 2013, 08:24:25 AM »
Thank you guys for the positive feedback! Also, thank you for introducing Curses to me, that looks really interesting to work with. I'll get right on that introduction  :)

3
Scripting Languages / [Python] Tic Tac Toe
« on: May 23, 2013, 02:58:38 AM »
Hello Evilzone! First time poster, short time lurker here.  I have recently started digging into programming and went with Python! Just for educational purposes on my behalf, I want to post this Tic Tac Toe game I have been working on! I am open to any suggestions to improve what may seem like redundant code. Thanks in advance!

Code: (python) [Select]
from random import randint
import time
import os #For pretty color reasons

raw_input("Welcome to tic-tac-toe! Press Entert to start:")
board = []
numTries, player_wins, computer_wins = 0, 0, 0

def makeBoard():
    global board
    for i in range(0, 10):
        board.append("[ ]")

def printBoard():
    #Print the game
    global board
    print board[1], board[2], board[3]
    print board[4], board[5], board[6]
    print board[7], board[8], board[9]
   
def move():
    #Prompt the player to make a move
    os.system("color a")
    global numTries
    numTries = 0
   
    #Take input and check if spot is available
    try:
        x = int(raw_input("\nIt's your turn(1-9):"))
        if not board[x] == "[X]" and not board[x] == "[O]":
            board[x] = "[X]"
        else:
            print "That spot is already chosen."
            move()
    except:
        print("You must have screwed up.")
        move()
    printBoard()
    checkForWin()
    time.sleep(.5)
    computeMove()
   
def computeMove():
    #Generate random spot on the board, make move if available
    os.system("color c")
    global numTries
    global board
    numTries += 1
    selection = randint(1, 9)
    if not board[selection] == "[X]" and not board[selection] == "[O]":
        board[selection] = "[O]"
        print ("\nComputer has chosen.")
   
    #AI - Place move against you -- each row has 3 combinations
    #Only executes if random move is unsuccessful on first try
    #Row 1
    elif board[1] == "[X]" and board[2] == "[ ]" and board[3] == "[X]":
        board[2] = "[O]"
    elif board[1] == "[ ]" and board[2] == "[X]" and board[3] == "[X]":
        board[1] = "[O]"
    elif board[1] == "[X]" and board[2] == "[X]" and board[3] == "[ ]":
        board[3] = "[O]"
    #Row 2
    elif board[4] == "[X]" and board[5] == "[ ]" and board[6] == "[X]":
        board[5] = "[O]"
    elif board[4] == "[ ]" and board[5] == "[X]" and board[6] == "[X]":
        board[4] = "[O]"
    elif board[4] == "[X]" and board[5] == "[X]" and board[6] == "[ ]":
        board[6] = "[O]"
    #Row 3
    elif board[7] == "[X]" and board[8] == "[ ]" and board[9] == "[X]":
        board[8] = "[O]"
    elif board[7] == "[ ]" and board[8] == "[X]" and board[9] == "[X]":
        board[7] = "[O]"
    elif board[7] == "[X]" and board[8] == "[X]" and board[9] == "[ ]":
        board[9] = "[O]"
    #Diagonal 1
    elif board[1] == "[X]" and board[5] == "[ ]" and board[9] == "[X]":
        board[5] = "[O]"
    elif board[1] == "[ ]" and board[5] == "[X]" and board[9] == "[X]":
        board[1] = "[O]"
    elif board[1] == "[X]" and board[5] == "[X]" and board[9] == "[ ]":
        board[9] = "[O]"
    #Diagonal 2
    elif board[3] == "[X]" and board[5] == "[ ]" and board[7] == "[X]":
        board[5] = "[O]"
    elif board[3] == "[ ]" and board[5] == "[X]" and board[7] == "[X]":
        board[3] = "[O]"
    elif board[3] == "[X]" and board[5] == "[X]" and board[7] == "[ ]":
        board[7] = "[O]"
    else:
        if numTries > 20:
            raw_input("It's a cat's game!")
            restart()
        computeMove()
    printBoard()
    checkForWin()
    move()
   
def checkForWin():
    #Define win combinations - 8 possible for each X and O
    global board
    global numTries
    global player_wins
    global computer_wins
    if board[1] == "[X]" and board[2] == "[X]" and board[3] == "[X]":
        raw_input("You win!")
        player_wins += 1
        restart()
    if board[4] == "[X]" and board[5] == "[X]" and board[6] == "[X]":
        raw_input("You win!")
        player_wins += 1
        restart()
    if board[7] == "[X]" and board[8] == "[X]" and board[9] == "[X]":
        raw_input("You win!")
        player_wins += 1
        restart()
    if board[1] == "[X]" and board[4] == "[X]" and board[7] == "[X]":
        raw_input("You win!")
        player_wins += 1
        restart()
    if board[2] == "[X]" and board[5] == "[X]" and board[8] == "[X]":
        raw_input("You win!")
        player_wins += 1
        restart()
    if board[3] == "[X]" and board[6] == "[X]" and board[9] == "[X]":
        raw_input("You win!")
        player_wins += 1
        restart()
    if board[1] == "[X]" and board[5] == "[X]" and board[9] == "[X]":
        raw_input("You win!")
        player_wins += 1
        restart()
    if board[3] == "[X]" and board[5] == "[X]" and board[7] == "[X]":
        raw_input("You win!")
        player_wins += 1
        restart()
   
    #Computer's combinations
    if board[1] == "[O]" and board[2] == "[O]" and board[3] == "[O]":
        raw_input("You LOSE!")
        computer_wins += 1
        restart()
    if board[4] == "[O]" and board[5] == "[O]" and board[6] == "[O]":
        raw_input("You LOSE!")
        computer_wins += 1
        restart()
    if board[7] == "[O]" and board[8] == "[O]" and board[9] == "[O]":
        raw_input("You LOSE!")
        computer_wins += 1
        restart()
    if board[1] == "[O]" and board[4] == "[O]" and board[7] == "[O]":
        raw_input("You LOSE!")
        computer_wins += 1
        restart()
    if board[2] == "[O]" and board[5] == "[O]" and board[8] == "[O]":
        raw_input("You LOSE!")
        computer_wins += 1
        restart()
    if board[3] == "[O]" and board[6] == "[O]" and board[9] == "[O]":
        raw_input("You LOSE!")
        computer_wins += 1
        restart()
    if board[1] == "[O]" and board[5] == "[O]" and board[9] == "[O]":
        raw_input("You LOSE!")
        computer_wins += 1
        restart()
    if board[3] == "[O]" and board[5] == "[O]" and board[7] == "[O]":
        raw_input("You LOSE!")
        computer_wins += 1
        restart()
       
def whoStarts():
    #If even number you start, odd number computer starts
    i = randint(0, 10)
    if i % 2 == 0:
        move()
    else:
        computeMove()
       
def restart():
    #Restart the game
    global board
    global player_wins
    global computer_wins
    os.system("color f")
    board = []
    print "\nYour score: ", player_wins
    print "Computer score: ", computer_wins
    raw_input("\n~~~~~~~~~~~~~~~~~~~~~~~~~Press Enter to play again!~~~~~~~~~~~~~~~~~~~~~~~~~")
    makeBoard()
    whoStarts()
   
if __name__ == "__main__":
    makeBoard()
    whoStarts()


Pages: [1]


Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.