Took me a couple days and I had to make 2 versions. Version 1.2 is better looking and more readable, with better functionality. No where near optimized though, as I know nothing about that. Save and Load Functionality won't work on your OS unless you change the file paths in the code, but those took me a while to get working on mine.
Always looking for ways to make my code better.
#v1.2 Final draft. functional shell Rock-Paper-Scissors Game
#- Rock-Paper-Scissors Game
#- Beginner Project
#- Functional file save
#- Functional file import
#-----------------------------
# TO DO LIST
#-----------------------------
#- Web scrape for win and loss
# quotes
#- Gui (Big MAYBE, don't know
# how to yet)
#- File save and load to work on every os
#imports
import random
import os
from time import sleep
#dictionary holding game choices
rps_list = {
'rock': 'rock',
'paper': 'paper',
'scissors': 'scissors'
}
#saves player data to file and names file using data input
def player_file_save(player):
saving = True
#loop in case of invalid input
while saving:
#checks if file doesn't exists
if not os.path.exists(f'/home/daniel/Documents/Coding/{player.data[0]}_save'):
#creates new files with player_dataw
with open(f'{player.data[0]}_save', 'w+') as file:
file.write(f'Username: {player.data[0]}\nDifficulty: {player.data[1]}\nWin Streak: {player.data[2]}')
print('File Saved!')
break
else:
#if player wants to overwrite file
continue_save = input('\nThis player save file already exist. Would you like to overwrite[Yes or No]? ')
#if answer isn't yes
if not (continue_save.lower() == 'yes' or continue_save.lower() == 'y'):
#if answer isn't no
if not(continue_save.lower() == 'no' or continue_save.lower() == 'n'):
print('\nNot a valid option!')
continue
#if answer is no breaks out of loop
else:
print('\nFile Save Aborted!')
break
#if answer is yes, overwrites previously existing file
else:
with open(f'{player.data[0]}_save', 'w+') as file:
file.write(f'Username: {player.data[0]}\nDifficulty: {player.data[1]}\nWin Streak: {player.data[2]}')
print('\nFile Saved')
break
#loads player file and stores it in player.data
def file_load(player, player_name = 'Default'):
#if file doesn't exists
if not (os.path.exists(f'/home/daniel/Documents/Coding/{player_name}_save')):
print("\nThis save file doesn't exist")
return None
#checks if the file exists
else:
#writes every stat to the file
with open(f'{player_name}_save', 'r+') as file:
#writes username
username = ''.join(file.readline()).strip('\n')[10::]
player.modify_stats('Username', username, 'edit')
#writes difficulty
difficulty = int(''.join(file.readline()).strip('\n')[12::])
player.modify_stats('Difficulty', difficulty, 'edit')
#writes win_streak
win_streak = int(''.join(file.readline()).strip('\n')[11::])
player.modify_stats('Win_streak', win_streak, 'edit')
print('\nSave Data Loaded!')
#player class. Each instance uses player_data index variables during initialization
class Player():
#initialization of stats for player
def __init__(self, player_data):
self.username = player_data[0]
self.difficulty = player_data[1]
self.win_streak = player_data[2]
self.data = [self.username, self.difficulty, self.win_streak]
#replace player statistics
def modify_stats(self, stat, value, mode):
#if mode is invalid
if not (mode == 'edit'):
if not (mode == 'tweak'):
pass
else:
#if stat isn't difficulty/win_streak
if not(stat == 'Win_streak'):
pass
#if stat is win_streak
else:
self.win_streak += value
#set self.data to updated values
self.data = [self.username, self.difficulty, self.win_streak]
else:
#if stat isn't username/difficulty/win_streak
if not (stat == 'Username'):
if not(stat == 'Difficulty'):
if not(stat == 'Win_streak'):
pass
#if stat is win_streak
else:
self.win_streak = value
#if stat is difficulty
else:
self.difficulty = value
#if stat is username
else:
self.username = value
#set self.data to updated values
self.data = [self.username, self.difficulty, self.win_streak]
#returns player choice
def play(self):
playing = True
while playing:
choice = input('[Rock:Paper:Scissors] Which will you play?')
if not(choice.lower() == 'rock' or choice.lower() == 'r'):
if not(choice.lower() == 'paper' or choice.lower() == 'p'):
if not(choice.lower() == 'scissors' or choice.lower() == 's'):
print("This isn't a valid option")
continue
else:
print('\nYOU HAVE CHOSEN SCISSORS!')
return rps_list['scissors']
else:
print('\nYOU HAVE CHOSEN PAPER!')
return rps_list['paper']
else:
print('\nYOU HAVE CHOSEN ROCK!')
return rps_list['rock']
def __str__(self):
return f'\nUsername: {self.username}\nDifficulty: {self.difficulty}\nWin Steak: {self.win_streak}\n'
#returns bot choice
def bot_play(player, player_choice, choice = rps_list):
num = random.randint(1,3)
#if player difficulty is 1, player has highest chance of winning
if player.data[1] == 1:
if num == 1:
return choice['rock']
elif num == 2:
return choice['paper']
else:
return choice['scissors']
#if player difficulty is 2, player's chance of winning is 2/3
elif player.data[1] == 2:
if not(num == 1 or num == 3):
if player_choice.lower() == 'rock' or player_choice.lower() == 'r':
return choice['paper']
elif player_choice.lower() == 'paper' or player_choice.lower() == 'p':
return choice['scissors']
elif player_choice.lower() == 'scissors' or player_choice.lower() == 's':
return choice['rock']
else:
if num == 3:
return choice['rock']
elif num == 1:
return choice['paper']
elif num == 2:
return choice['scissors']
#if player difficulty is 3, player's chance of winning is 1/3
elif player.data[1] == 3:
if not(num == 1 or num == 3):
if num == 1:
return choice['rock']
elif num == 3:
return choice['paper']
elif num == 2:
return choice['scissors']
else:
if player_choice.lower() == 'rock' or player_choice.lower() == 'r':
return choice['paper']
elif player_choice.lower() == 'paper' or player_choice.lower() == 'p':
return choice['scissors']
elif player_choice.lower() == 'scissors' or player_choice.lower() == 's':
return choice['rock']
#NO CHANCE OF WINNING. HAHAHAHHAAH. Troll setting if you want to reset your win_streak built up by sheer luck
else:
if player_choice.lower() == 'rock' or player_choice.lower() == 'r':
return choice['paper']
elif player_choice.lower() == 'paper' or player_choice.lower() == 'p':
return choice['scissors']
elif player_choice.lower() == 'scissors' or player_choice.lower() == 's':
return choice['rock']
init = True
while init:
player = Player(['Default', 1, 1])
initialization = input('\nNew Game[Yes or No]: ').lower()
if not(initialization == 'yes' or initialization == 'y'):
if not(initialization == 'no' or initialization == 'n'):
print('\nNot an option')
else:
username = input('\nWhose save data are you loading? ')
if os.path.exists(f'/home/daniel/Documents/Coding/{username}_save'):
file_load(player, username)
init = False
else:
username = input('\nWhat is your username? ')
player = Player([username, 1, 0])
print('\nNew game started!')
init = False
print(player)
running = True
while running:
mode = input('\n|| Play || Save || Load || Stats || Close ||: ').lower()
if not(mode == 'play'):
if not(mode == 'save'):
if not(mode == 'load'):
if not(mode == 'stats'):
if not(mode == 'close'):
#if option is not valid
print('\nNot an option!')
else:
#if mode is close
last_check = input('\nDo you want to save your file before exiting[Yes or NO or Cancel]? ').lower()
closing = True
while closing:
if not(last_check == 'yes' or last_check == 'y'):
if not(last_check == 'no' or last_check == 'no'):
if not(last_check == 'cancel' or last_check == 'c'):
print('\nNot an option!')
else:
closing = False
else:
exit()
else:
player_file_save(player)
exit()
else:
#if mode is stats
print(player)
stat_change_check = True
while stat_change_check:
stat_change_confirmation = input('\nDo you want to change difficulty[Yes or No]? ').lower()
if not(stat_change_confirmation == 'yes' or stat_change_confirmation == 'y'):
if not(stat_change_confirmation == 'no' or stat_change_confirmation == 'n'):
print('\nNot a valid option')
else:
stat_change_check = False
else:
diff_check = True
while diff_check:
diff_change = int(input('\nWhat difficulty would you like to chnge to[1,2,3,4]: '))
if diff_change != 1 and diff_change != 2 and diff_change != 3 and diff_change != 4:
print('\nNot an option')
else:
player.modify_stats('Difficulty', diff_change, 'edit')
diff_check = False
stat_change_check = False
else:
#if mode is load
loading = True
while loading:
input_confirm = input('\nDo you want to load another game[Yes or No]? ').lower()
if not(input_confirm == 'yes' or input_confirm == 'y'):
if not(input_confirm == 'no' or input_confirm == 'n'):
print('\nNot an option!')
else:
loading = False
else:
username = input('\nWhat was your username? ')
file_load(player, username)
loading = False
else:
#if mode is save
player_file_save(player)
else:
#if mode is play
playing = True
while playing:
player_choice = player.play()
bot_choice = bot_play(player, player_choice)
if not(bot_choice == rps_list['paper']):
if not(bot_choice == rps_list['rock']):
#if bot plays scissors
print('BOT HAS PLAYED SCISSORS')
if not(player_choice == rps_list['paper']):
if not(player_choice == rps_list['rock']):
#if you played scissors
print('\nYou have tied with the bot! Win streak remains')
else:
#if you played rock
print('\nYou have won against the bot! Win streak has gone up!')
player.modify_stats('Win_streak', 1, 'tweak')
else:
#if you played paper
print('\nYou have lost against the bot! Win streak reset')
player.modify_stats('Win_streak', 0, 'edit')
else:
#if bot played rock
print('BOT HAS PLAYED ROCK')
if not(player_choice == rps_list['paper']):
if not(player_choice == rps_list['rock']):
#if you played scissors
print('\nYou have lost against the bot! Win streak reset')
player.modify_stats('Win_streak', 0, 'edit')
else:
#if you played rock
print('\nYou have tied with the bot! Win streak remains')
else:
#if you played paper
print('\nYou have won against the bot! Win streak has gone up!')
player.modify_stats('Win_streak', 1, 'tweak')
else:
#if bot played paper
print('BOT HAS PLAYED PAPER')
if not(player_choice == rps_list['paper']):
if not(player_choice == rps_list['rock']):
#if you played scissors
print('\nYou have won against the bot! Win streak has gone up!')
player.modify_stats('Win_streak', 1, 'tweak')
else:
#if you played rock
print('\nYou have lost against the bot! Win streak reset')
player.modify_stats('Win_streak', 0, 'edit')
else:
#if you played paper
print('\nYou have tied with the bot! Win streak remains')
#checks if you want to keep playing
cont_check = True
while cont_check:
cont = input('\nWould you like to keep playing[Yes or No]? ')
if not(cont.lower() == 'yes' or cont.lower() == 'y'):
if not(cont.lower() == 'no' or cont.lower() == 'n'):
print('\nNot a valid option')
else:
cont_check = False
playing = False
else:
cont_check = False
mode
checking. \$\endgroup\$if
statement for every possible combination. Your code becomes super long... If you convertrock,paper,scissors
to0,1,2
you can already simplify your code a lot. \$\endgroup\$