Python Forum
Assign 1 player to the computer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assign 1 player to the computer
#1
Hi all,

Nice to be here :) I'm brand new to python so please be nice!

I have this 'last one loses' game to do. I cannot, for the life of me, get 1 player to be the computer so basically it's me against the computer. The computer choses a number randomly between 1 & 3. It's based on the game of nim. I have it working so both players require human input, but I want it so 1 is human and 1 is the computer. Any idea where i'm going wrong please?

#last one loses game

#import random module
import random

#assign random play to the computer
computer = (random.randint(1,3))

#set player to false
player = False

#set initial state
state = 0
while state < 10 or state > 51:
    state = int(input("Enter counter number between 10 and 50 "))
    
#initial number of counters
print ("The number of counters is ", state)

#get valid move and set player to True
while player == False:
    if state == 1:
        print("The computer has taken 1 counter")
    if state == 2:
        print("The computer taken 2 counters")
    if state == 3:
        print("The computer chosen 3 counters")
        remainingCounters(state)

    print("player ",player)
    while True:
        move=int(input("How many counters would you like to remove? "))
        if move in [1,2,3] and move<state:
            break
        print("Please only choose up to 3 counters!")
    
    #update state
    state=state-move


    #show state
    print("The remaining number of counters is now ", state)


    #check win status - win or lose
    if state == 1:
        print("Player ",player, " wins!")
        break

    #switch players 2->1, 1->2 then go back to valid move line
    ##if player==1:
   ## player=2
   ## else:
      ##  player=1

print("Game over")
Reply
#2
First of all, while loop condition is wrong

state = 0
while state > 10 or state < 51:
    state = int(input("Enter counter number between 10 and 50 "))

please tell me how the game works?
Reply
#3
Your player = False and the accompanying logic at the start of your loop (lines 21-28) isn't really accomplishing anything at this point. In a game with only two players, you don't necessarily need to use a variable to keep track of whose turn it is. A simple way to handle having one human and one computer player is to include both turns in your main game loop. You get the player's move, apply it, check if the game is over, then do the same for the computer move. Break out when the game ends.

This pseudocode is not at all complete, but gives you the basic idea:

while True:
    print('Number of counters', state)

    human_move = int(input('Enter your move'))
    state = state - human_move
    if state == 1:
        print('Human wins')
        break

    print('Number of counters', state)

    computer_move = random.randint(1, 3)
    print('Computer move is', computer_move)
    state = state - computer_move
    if state == 1:
        print('Computer wins')
        break
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My code won't say Player wins or computer wins. No errors. its_a_meLuigi64 2 1,587 Dec-01-2021, 04:43 PM
Last Post: its_a_meLuigi64

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020