Swapping and removing numbers within a randomized Python guessing game - python

Within a python program, I need to simulate a basic game. Within this game, I need to randomize a hiding spot using a number between 1-3 as part of a simulated game:
def coin_places():
return random.randrange(1,4)
I then take this random number and put it into the game, seeing if the simulated guess is correct:
hide_spot = coin_places()
print(hide_spot)
guess = random.randrange(1,4)
print(guess)
def game():
if guess == hide_spot:
return True
elif guess != hide_spot:
return False
else:
return game(guess)
However, where I'm stumped is, I've been tasked with creating a caveat where one wrong hiding spot is removed, and the program must swap the remaining two guess numbers.
For example, the ramdom output is guess = 2 and hide_spot = 2, so the number 3 or 1 needs to be removed, and guess will be swapped with what is left, let's say from 2 to 1. Each the caveat and original values are outputted as percent chances (just as background, I'm able to write that part)
I can't seem to be able to find any examples of this kind of problem elsewhere, and any help would be greatly appreciated!
To be a bit more clear essentially I need to:
Create a simulated number of games, from user input
Create the number of spots and designate one as the hiding spot.
Create the parameters of the normal game, simulating user guess
Add another technique, which will produce its own results, where one wrong hiding spot is removed, and the program must swap the remaining two guess numbers.
Output the chances of success of each technique as percentages
Thanks a bunch for any help or consideration!

Related

What modules should I use to make my grid swap letters as intended?

I have a crossword puzzle project. I have a puzzle and a random letter generator for every time I press run. Here is the code,
import random
import string
for x in range(4):
for y in range(4):
print( '['+random.choice(string.ascii_letters)+']',end='' )
print()
This results in the following output
[O][E][R][i]
[g][c][C][J]
[D][M][Z][X]
[y][A][M][Q] (letters are randomly generated)
I want to take it to the next level by implementing code which will swap the places of two blocks in the grid which are either exactly above, below, to the left or to the right of each other. Furthermore, I want this to be done only through touch, not any commands
Through research I have learnt that some modules could be used such as the Kivy module or the touch module but I'm not entirely sure. Can anyone please suggest what should I check to get started on this?

How do I get this input to work inside of an inherited class in Python?

So, to preface this I’m self learning python and I’m trying to build a Tic-Tac-Toe game using the command line as an interface. The issue that I have is that I can’t get the input inside one of the inherited class for the player to work (so the game itself doesn’t work aside from the 3x3 board showing up on the command line)
The section of code that I’m having issues with goes as such:
class HumanPlayer(Player):
def __init__(self, letter):
super().__init__(letter)
def get_move(self, game):
valid_square = False
val = None
while not valid_square:
square = input(self.letter + ' s turn. Input move (0-9):')
# we are going to check that this is a correct value by trying to cast
# it into an integer, and if it's not, then we will say its invalid
# if that spot is not available on the board, then we also say it's invalid
try:
val = int(square)
if val not in game.available_moves:
raise ValueError
valid_square = True # if these are successful, then oh yeah!
except ValueError:
print ('Invalid Square. Try Again. ')
return val
I’ve tried to make sure that my spacing is correct within this class, but now I’m not sure what else to do. Any help, suggestions, or the like would be appreciated since I’m learning to program in general
Thanks!
Although there is nothing wrong with an object-oriented approach, and it can be the (or at least a) right approach for many problems, it looks like your program has "classes because of classes". It's probably easier if you don't bother with the object-orientation too much at this stage and focus on the main gameplay loop.
Try to imagine how the game should progress: you start the game, you make a move, another player makes a move, this continues until the game decides either play has won and then perhaps you can start a new game. And the other player might be an "AI" (tic tac toe doesn't require much intelligence) or another live player.
Your code covers what needs to happen for a single player to enter a valid square and it appears you have another class somewhere that's a Game, an instance of which has an attribute available_moves that contains all the currently valid moves. Or perhaps that Game class is the next thing you plan to write.
The main game loop would be to ask players for a move in alternating fashion, update the game board, decide if someone has won yet, and keep doing that. And you'll need to get the whole thing started, some core routine that sets up the game and the players and gets the ball rolling.
If you have a more specific problem getting all that to work, you should post a question about that - but without a more specific problem, it's hard to provide a better answer.
Where is the code for th

Is there a way to get legal moves for the person whos turn its not? python-chess

I am interested in making a chess algorithm. For this, I will be using the python-chess library. However, to make a good algorithm I need to be able to return the opposing persons legal moves even if it isn't their turn.
So in the start of the game it would return
board.legal_moves() -> [A2A4, A2A1, B2B4, B2B1, ect...]
I was wondering if during the start of the game I could say something like
board.enemy_legal_moves() > [A7A5, A7A6, ect...]
or if there is any algorithmic way I could do it.
Thank you for your time!
A solution would be to keep track of the legal moves in a list from the beginning of the game:
legal_moves = []
current_legal_moves = board.legal_moves()
legal_moves.append(current_legal_moves)
Then you can simply get the list of your enemy's legal moves by:
enemy_legal_moves = legal_moves[-1]
This will obviously not work if you want to call this for the first move in the game. If you really need to do that, then you can either:
From the start position switch to black and run the board.legal_moves(). Then take this list and add as start value like this: legal_moves = [black_legal_moves_start_pos]
Write the list of blacks 20 first legal moves from the start position and add it to legal_moves as in option 1 above.

Finding Next Obstacle in List

I am trying to create a game that moves to the end of a square. Inside the game I created obstacles by making a list of true and false (false=obstacles). What I have (but did not post) is something that detects an obstacle ONLY IF the user lands on the obstacle. However, I want to find a way to detect an obstacle BEFORE the user makes its next move and make it stay in place if the next spot will be an obstacle. In other words I want to find the next index of list before proceeding. Here's some pseudo code for a better picture:
if next_left != [[False]]: # if there is no obstacle
officially_move_left
else: # if there is an obstacle
user_do_nothing
What I have (in pseudo):
def moving_pos(user):
copy_user.pos = user.pos
if copy_user.pos +1 == [True]
user.pos += 1
copy_user.pos += 1
else:
return user.pos
if (user_position == obstacle):
next_square_is_obstacle = true
user_move_backwards
ie, move the player, detect the object, move the player back again. It's very difficult to help without seeing the actual implementation
Edit: Okay, in your new edit I don't see any reason why the program should have such constraints. However, let me suggest something new-
obstacle_index = [obs1, obs2, obs3...]
if user_position+1 in obstacle_index:
do_nothing;
else
user_move_forward;
Can you post the actual minimal working code, or at least a more detailed pseudocode of the implementation?

How to execute code in python to send the terminal to another file

I'm writing a text based adventure game in python and I can't hold all of the dialog choices in one file. I'm making the choices in int(input("blah")) and if statements.
Basically I give 3 choices, and tell them to choose a number, 1-3. If they choose 1, the choice that matches one shows on the terminal, and so on. Now that I've made one level of dialog I can't hold two levels.
For example: I give them 3 choices. I write an if statement so if they input 1 in the terminal, 'Blah' comes on the screen. Then, from that, I give 3 more choices.
I cant do this though, because it will just execute the code at the end of the statements. I need to be able to make different documents so when they choose a different option, I can handle the load of data they entered, and they can have more then one level of adventure gameplay.
Edit: Is there any way this is possible?
The code looks a little like this:
choice1 = int(input("Choose a number "))
if choice1 == 1:
cho1_1()
if choice1 == 2:
cho1_2()
if choice1 == 3:
cho1_3()
def cho1_3():
print ("'Say anything like that again to me and the only thing you'll see is the damp ceiling of my cellars.'")
I think, the good solution is to create your own data files for each level.
Those files should contain following objects:
Rooms
Itmes
Enemies
Quests
etc...
Room object will hold some instances of Items, Enemies...etc
For example if you enter to some room, and got dialog "Choose your direction"
Depend on direction selection, your game will load other room with it's own data.
When you moving to next level just load new data file.
So what you have to do first if to carefully design you level data file.

Categories

Resources