4 Replies - 3829 Views - Last Post: 02 August 2012 - 01:51 PM Rate Topic: -----

#1 heyhihello  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-August 12

How to program my game to ask 'Would you like to play again?'

Posted 02 August 2012 - 01:07 PM

Okay, so I've been coding a Tic Tac Toe game in Python 3.2.3. So, I was curious, is there any way I can code my game at the end after the computer or person has one to ask the player if they want to play again? I've tried my best and googled everything, but a majority of them just haven't worked out. And I was wondering if you guys could help me? I want to program my game to say like, "Would you like to play again? Y or N:" and if the person types a Y, have the game automatically restart and if they type a N, just have the program quit and exit. Thanks so much! Oh and here's my code so far:

import random
import time

board = [0,1,2,
         3,4,5,
         6,7,8]

def show():
    print (board[0],"|",board[1],"|",board[2])
    print ("----------")
    print (board[3],"|",board[4],"|",board[5])
    print ("----------")
    print (board[6],"|",board[7],"|",board[8])

def checkLine(char, spot1, spot2, spot3):
    if board[spot1] == char and board[spot2] == char and board[spot3] ==char:
        return True

def checkAll(char):
    if checkLine(char, 0, 1, 2):
        return True
    if checkLine(char, 1, 4, 7):
        return True
    if checkLine(char, 2, 5, 8):
        return True
        
    if checkLine(char, 6, 7, 8):
        return True
    if checkLine(char, 3, 4, 5):
        return True
    if checkLine(char, 1, 2, 3):
        return True

    if checkLine(char, 2, 4, 6):
        return True
    if checkLine(char, 0, 4, 8):
        return True


while True:

    answer = input("Select a spot, any number 0 through 8:")
    answer = int(answer)

    time.sleep(1)

    if board[answer] != 'x' and board[answer] !='o':
        board[answer] = 'x'

        if checkAll('x') == True:
            print ("You win! Yay!")
            break;

        while True:
            random.seed()
            opponent = random.randint(0,8)

            if board[opponent] != 'o' and board[opponent] != 'x':
                board[opponent] = 'o'

                if checkAll('o') == True:
                    print ("You loose! That's to bad.")
                    break;

                break;

    else:
        print ("This spot is taken!")

    show()



Is This A Good Question/Topic? 0
  • +

Replies To: How to program my game to ask 'Would you like to play again?'

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9057
  • View blog
  • Posts: 34,001
  • Joined: 12-June 08

Re: How to program my game to ask 'Would you like to play again?'

Posted 02 August 2012 - 01:12 PM

It's pretty much just a do-while or while loop. Right before your code exits ask that question.. read the input.. if they click 'y' then the loop repeats.. if 'n' then the loop exist and so does the program.
Was This Post Helpful? 0
  • +
  • -

#3 heyhihello  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-August 12

Re: How to program my game to ask 'Would you like to play again?'

Posted 02 August 2012 - 01:17 PM

View Postmodi123_1, on 02 August 2012 - 01:12 PM, said:

It's pretty much just a do-while or while loop. Right before your code exits ask that question.. read the input.. if they click 'y' then the loop repeats.. if 'n' then the loop exist and so does the program.

But what would the code be and where would it go?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9057
  • View blog
  • Posts: 34,001
  • Joined: 12-June 08

Re: How to program my game to ask 'Would you like to play again?'

Posted 02 August 2012 - 01:22 PM

Like I said... a DO-WHILE loop would work... and it would wrap your existing code.. you would need to add two lines (one to ask to play again and one to read in the user input) before the start of the condition of the DO-WHILE.

Loops are fairly basic things - I am certain you can read up on them.
Was This Post Helpful? 0
  • +
  • -

#5 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: How to program my game to ask 'Would you like to play again?'

Posted 02 August 2012 - 01:51 PM

it's as modi123_1 says. You already have your game that can run once. Just put the whole of it in a function and create a main loop that runs again and again until the players says "N":

def game():
    pass #your game goes here, whatever it is

#main loop
play_again = "Y"
while play_again == "Y":
    game()
    play_again = input("do you want to play again?(Y/N)")


This post has been edited by Nallo: 02 August 2012 - 01:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1