Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Reseting Environment & Muting Erroneous Error Notifications
- clear;clc; % Clears the command window and all variables.
- %#ok<*SAGROW>
- %#ok<*AGROW>
- %#ok<*NOPTS>
- % Log
- if exist('Log.txt','file') == 2
- log_old = csvread('Log.txt'); log_new = clock;
- log = [log_old;log_new];
- csvwrite('Log.txt',log);
- else
- csvwrite('Log.txt',clock);
- end
- % Choice Variable Values
- rock = 1;
- paper = 2;
- scissors = 3;
- % Outcome/Player Identification Values Assigned
- user_n = 1;
- comp_n = 2;
- draw_n = 3;
- none_n = 0;
- % Commonly Used Strings Assigned to Variables
- markers = {'start';'end'};
- markerz = {0;0};
- end_Cell = {0,0,0,0,0};
- main = 0;
- while main == 0 % Loop Allowing User to Return to Login/Register Menu (From Later Points in Program)
- % Clear Variables from Last Iteration
- clearvars -except main rock paper scissors user_n comp_n draw_n none_n markers markerz end_Cell % Clears all data specific to any previous session/iteration (Not Constants)
- % Check for File with User Data
- if exist('Users.txt','file') == 2 % File 'Users.txt' (File with User Information) exists
- % continue
- else % File 'Users.txt' doesn't exist
- writetable(table(markers,markers,markers,'VariableNames',{'User';'Pass';'Role'}),'Users.txt'); % Create File 'Users.txt' to Store User Login Data
- mkdir('RSP/','Users'); % Create Directory 'Users' to Store User History Data Files
- end
- % Login/Register Loop
- login = 0;
- while login == 0 % Loop Allowing User to Return to Login/Register Menu (From Login/Register Point in Program)
- read_Users = readtable('Users.txt','ReadVariableNames',true); % Gather All User Login Data for Later Use
- login_menu = menu('Login Menu','Registered User','New User','Quit'); % Prompt User to Login, Create User, or Quit Program
- switch login_menu % User Login/Register Menu Choice
- case 1 % User Chose Registered User
- creds{1,1} = input('Input Username: ','s'); % User Inputs Supposed Registered Username
- creds_counter = 0; % Counter Recording Number of Rows Checked for User Input Username is Created/Reset
- valid_user = 0;
- while valid_user == 0 % Loop Continues Until User Input Username is Deemed Valid or Invalid
- creds_counter = creds_counter + 1; % Counter Increases to Account for Checking of an Additional Row
- if strcmp(creds{1,1},'start') || strcmp(creds{1,1},'end') % If User Input Username Matches Indicator Strings (Indicating First/Last Rows in Table/Cell)
- valid_user = 2; % User Input Username Marked Invalid (No Username May Match Indicator Strings)
- elseif strcmp(char(read_Users.User(creds_counter)),creds{1,1}) % If User Input Username Matches Stored User Login Data (Username is Registered)
- valid_user = 1; % User Input Username Marked Valid
- elseif strcmp(char(read_Users.User(creds_counter)),'end') % If Indicator String 'end' (Indicating Last Row in Table/Cell) is Reached (User Input Username Matches No Stored User Data)
- valid_user = 2; % User Input Username Marked Invalid
- else % strcmp(char(read_Users.User(creds_counter)),creds{1,1}) = false % If Stored User Login Data on Current Row Doesn't Match User Input Username
- % continue
- end
- end
- switch valid_user % User Input Username Validity
- case 1 % User Input Username is Valid
- valid_pass = 0;
- while valid_pass == 0 % Loop Continues Until User Inputs Correct Password (For Input Username User) or Stops Trying After Incorrect Input
- creds{1,2} = input('Input Password: ','s'); % User Inputs Supposed Correct Password (For Input Username User)
- clc; % Clears Command Window
- if strcmp(char(read_Users.Pass(creds_counter)),creds{1,2}) % If User Input Password Matches Recorded Password for Input Username User
- fprintf('Welcome, %s.',creds{1,1}); % Display Personalized Welcome Message (Featuring Unique Username)
- valid_pass = 1; % User Input Password Marked Valid
- login = 1; % Condition for Login is Marked as Fulfilled Allowing Program to Continue (Past the Login/Register Section)
- else % strcmp(char(read_Users.Pass(creds_counter)),creds{1,2}) == false % If User Input Password Doesn't Match Recorded Password for Input Username User
- pass_menu = menu('Incorrect Password','Try Again','Main Menu'); % User Can Attempt to Input Correct Password (For Input Username User) Again or Stop Trying
- switch pass_menu % User Invalid Password Menu Choice
- case 1 % User Chose Try Again
- fprintf('Input Username: %s\n',creds{1,1}); % Input Username is Displayed to Mirror Previous Display
- case {2,0} % User Chose to Stop Trying
- valid_pass = 2; % User Input Password Marked Invalid
- end
- end
- end
- case 2 % User Input Username is Invalid
- clc; % Clears Command Window
- fprintf('Error: User ''%s'' does not exist.',creds{1,1}); % Display Invalid Username Error Message
- end
- case 2 % User Chose New User
- create_user = 0;
- while create_user == 0 % Loop Continues Until User Successfully Creates New User or Stops Trying After Invalid Input
- clc; % Clears Command Window
- fprintf('*Usernames/Passwords are case sensative and cannot be blank*\n\n'); % Display Parameters for User Input New Username and Password
- creds{1,1} = input('Create Username: ','s'); % User Inputs Supposed New Username Supposedly Within Previously Displayed (& Still Visible) Parameters
- creds_counter = 0; % Counter Recording Number of Rows Checked for User Input New Username is Created/Reset
- avail_user = 0;
- while avail_user == 0 % Loop Continues Until User Input New Username is Deemed Available or Unavailable
- creds_counter = creds_counter + 1; % Counter Increases to Account for Checking of an Additional Row
- if strcmp(creds{1,1},'start') || strcmp(creds{1,1},'end') % If User Input New Username Matches Indicator Strings (Indicating First/Last Rows in Table/Cell)
- avail_user = 2; % User Input New Username Marked Unavailable (No Username May Match Indicator Strings)
- elseif strcmp(char(read_Users.User(creds_counter)),'end') % If Indicator String 'end' (Indicating Last Row in Table/Cell) is Reached (User Input New Username Matches No Stored User Login Data)
- avail_user = 1; % User Input New Username Marked Available
- elseif strcmp(char(read_Users.User(creds_counter)),creds{1,1}) % If User Input New Username Matches Stored User Login Data (Username is Already Registered)
- avail_user = 2; % User Input New Username Marked Unavailable (No Username May Match Another)
- else % strcmp(char(read_Users.User(creds_counter)),creds{1,1}) = false % If Stored User Login Data on Current Row Doesn't Match User Input New Username
- % continue
- end
- end
- switch avail_user % User Input New Username Availability
- case 1 % User Input New Username Marked Available
- creds{1,2} = input('Create Password: ','s'); creds{1,3} = 'User'; % User Inputs Password Supposedly Within Previously Displayed (& Still Visible) Parameters (Input Stored in Cell) % User Role (Defining Permissions) is Set to 'User' by Default
- for creds_c = 1:3 % For All Columns in the New User Data Cell
- creds{2,creds_c} = 'end'; % Indicator String (Indicating Last Row in Table/Cell/File) Appended to New User Data Cell
- end
- read_Users = table2cell(read_Users); % Convert User Login Data Table to Cell to Enable Editing
- read_Users(size(read_Users,1),:) = []; % Delete Bottom Row of Cell to Enable Seamless Data Appending
- read_Users = [read_Users;creds]; % Append New Data (For New User) to User Login Data
- read_Users = cell2table(read_Users,'VariableNames',{'User';'Pass';'Role'}); % Convert User Login Data Cell to Table for File Writing
- writetable(read_Users,'Users.txt'); % Write User Login Data to User Login Data File
- hist_initial = table(markerz,markerz,markerz,markerz,markerz,'VariableNames',{'User';'Comp';'Result';'Diff';'Count'}); % Create Table Formatted for Storage of New User History
- writetable(hist_initial,strcat('RSP/Users/',creds{1,1},'.txt')); % Create Unique File for New User History & Write Formatted Table to File
- created_menu = menu('User Registered','Main Menu'); % User Informed of Successful New User Registration & Given Option to Return to Login/Register (Main) Menu
- switch created_menu % User Registration Confirmation Menu Choice
- case {1,0} % User Chose Login/Register (Main) Menu or Closed Menu
- clc; % Clears Command Window
- create_user = 1; % New User Registration Process Marked Complete & Successful (Condition is Met to End New User Registration Loop)
- end
- case 2 % User Input New Username Marked Unavailable
- clc; % Clears Command Window
- fprintf('Error: Username ''%s'' is unavailable.',creds{1,1}); % Display Unavailable Username Error Message
- create_menu = menu('Username Unavailable','Try Again','Main Menu'); % User Can Try New User Registration Again or Return to Login/Registration (Main) Menu (Stop Trying)
- switch create_menu % User Unavailable Username Menu Choice
- case 1 % User Chose Try Again
- % continue
- case {2,0} % User Chose Login/Register (Main) Menu or Closed Menu
- create_user = 2; % New User Registration Process Marked Complete & Unsuccessful (Condition is Met to End New User Registration Loop)
- end
- clc; % Clears Command Window
- end
- end
- case {3,0} % User Chose Quit or Closed Menu
- clear;clc; % Clears Command Window & All Variables
- return; % Ends Program
- end
- end
- loggedin = 1;
- while loggedin == 1
- counter = 0; % Reset Round Counter
- user_menu = menu(creds{1,1},'Play','Log Out','Quit'); % User Can Play, Log Out, or Quit Program
- switch user_menu % User Menu Selection
- case 1 % User Chose Play
- % Difficulty Level Selection
- diff = menu('Choose a difficulty level.','Normal (Random)','Realistic (Human-like)','Impossible (Cold & Calculated)'); % User Selects Difficulty Level
- % Loop for Continuous Play
- play = 1;
- while play == 1 % Loop Allowing User to Play Round After Round
- % Preparation for New Round
- read_Hist = readtable(strcat(creds{1,1},'.txt'),'ReadVariableNames',true); % User History Data is Gathered from Unique User File
- winner = none_n; % Winner Reset to None
- counter = counter + 1; % Counter Increased to Represent New Round Number
- for k = user_n:draw_n % For All Possible Winners
- score_log(counter,k) = 0; % Row for New Round Data is Appended to Score Log
- end
- for k = rock:scissors % For All Possible Player Choices
- choice_log(counter,k) = 0; % Row for New Round Data is Appended to Choice Log
- end
- % User Chooses Option
- user = menu('Rock, paper, or scissors?','ROCK','PAPER','SCISSORS','QUIT'); % User Can Select a Game Choice or Quit Game
- ucn = user; % User Choice is Recorded
- switch user % User Choice
- case rock % User Chose ROCK
- uc = 'ROCK'; % Choice String Assigned for Later Display
- case paper % User Chose PAPER
- uc = 'PAPER';
- case scissors % User Chose SCISSORS
- uc = 'SCISSORS';
- case {4,0} % User Chose to Quit Program
- clc; % Clear Command Window
- break; % Exit Play Loop
- end
- clc; % Clears Command Window
- fprintf('-------- %d --------- \n\n',counter); % Display Round Number
- fprintf('User chose '); % Display Label for User Choice
- disp(uc); % Display User Choice
- fprintf('\n'); % Move Cursor to Next Line
- % Computer Chooses Option
- switch diff % User Difficulty Choice
- case 1 % User Chose Random
- comp = randi(3); % Psuedo-Random Number from 1-3 Indicating Comp Choice
- case 2 % User Chose Realistic
- comp = RSP_StrategyDA(counter,table2cell(read_Hist)); % Function with Strategy Outputs Comp Choice
- case 3 % User Chose Impossible
- switch ucn % User Choice
- case rock % User Chose ROCK
- comp = paper; % Comp Chooses PAPER
- case paper % User Chose PAPER
- comp = scissors; % Comp Chooses SCISSORS
- case scissors % User Chose SCISSORS
- comp = rock; % Comp Chooses ROCK
- end
- end
- ccn = comp; % Comp Choice is Recorded
- switch comp % Comp Choice
- case rock % Comp Chose ROCK
- cc = 'ROCK'; % Choice String Assigned for Later Display
- case paper % Comp Chose PAPER
- cc = 'PAPER';
- case scissors % Comp Chose SCISSORS
- cc = 'SCISSORS';
- end
- fprintf('Computer chose '); % Display Label for Comp Choice
- disp(cc); % Display Comp Choice
- fprintf('\n'); % Move Cursor to Next Line
- % Determination of Winner
- switch user % User Choice
- case rock % User Chose ROCK
- switch comp % Comp Choice
- case rock % Comp Chose ROCK (DRAW)
- winner = draw_n; % Draw
- case paper % Comp Chose PAPER (COMP)
- winner = comp_n; % Comp Won
- case scissors % Comp Chose SCISSORS (USER)
- winner = user_n; % User Won
- end
- case paper % User Chose PAPER
- switch comp % Comp Choice
- case rock % Comp Chose ROCK (USER)
- winner = user_n;
- case paper % Comp Chose PAPER (DRAW)
- winner = draw_n;
- case scissors % Comp Chose SCISSORS (COMP)
- winner = comp_n;
- end
- case scissors % User Chose SCISSORS
- switch comp % Comp Choice
- case rock % Comp Chose ROCK (COMP)
- winner = comp_n;
- case paper % Comp Chose PAPER (USER)
- winner = user_n;
- case scissors % Comp Chose SCISSORS (DRAW)
- winner = draw_n;
- end
- end
- % Display of Winner
- switch winner
- case user_n % USER Won
- fprintf(' USER WINS \n\n'); % Display User Won
- case comp_n % COMP Won
- fprintf(' COMPUTER WINS \n\n'); % Display Comp Won
- case draw_n % DRAW
- fprintf(' DRAW \n\n'); % Display Draw
- end
- % Recording Score for Round
- score_log(counter,winner) = 1; % Record Round Winner
- % Recording Player Choices for Round
- choice_log(counter,user_n) = ucn; % Record User Choice
- choice_log(counter,comp_n) = ccn; % Record Comp Choice
- % Recording User History for Round
- hist_log = {choice_log(counter,1),choice_log(counter,2),winner,diff,counter}; % User Data for Current Round is Recorded
- read_Hist = table2cell(read_Hist); % Convert User History Table to Cell to Enable Editing
- read_Hist(size(read_Hist,1),:) = []; % Delete Bottom Row of Cell to Enable Seamless Data Appending
- read_Hist = [read_Hist;hist_log;end_Cell]; % Append New Data (For This Round) to User History
- read_Hist = cell2table(read_Hist,'VariableNames',{'User';'Comp';'Result';'Diff';'Count'}); % Convert User History Cell to Table for File Writing
- writetable(read_Hist,strcat(creds{1,1},'.txt')); % Write User History to User's File
- % Calculating Total Score of All Rounds
- for j = user_n:draw_n % For All Possible Winners
- score_total(1,j) = sum(score_log(:,j)); % Sum of Winner's Wins
- end
- % Displaying Score to User
- fprintf('------ Score ------- \n'); % Display Score Header
- fprintf('User Computer Draw \n'); % Display Score Field Labels
- fprintf('---- -------- ---- \n'); % Display Line Separating Labels/Header from Data
- fprintf('%d %d %d \n\n',score_total(1,user_n),score_total(1,comp_n),score_total(1,draw_n)); % Display Total Score for Each Winner
- end
- % Available Only to Admins (Users Marked Admins in User File)
- % if ((counter - 1) > 0) && (strcmp(char(read_Users.Role(creds_counter)),'Admin')) % User is Admin
- % % Optional Display of Records
- % records = menu('Display Score and/or Choice Records for Game?','SCORE & CHOICE','SCORE','CHOICE','NO'); % User Can Choose Whether or Not to Display Certain Game Data
- % switch records % User Record Menu Choice
- % case {1,2,3} % User Chose to Display Log(s)
- % fprintf('LEGEND: \n\n Column 1 - User \n Column 2 - Computer \n Column 3 - Draw \n'); % Display Legend to Enable Data Comprehension by User
- % switch records % User Record Choice
- % case 1 % Display SCORE & CHOICE Logs
- % score_log; % Display Score Log
- % choice_log; % Display Choice Log
- % case 2 % Display SCORE Log
- % score_log;
- % case 3 % Display CHOICE Log
- % choice_log;
- % end
- % case {4,0} % User Chose Not to Display Log(s)
- % % continue
- % end
- % else % User isn't Admin
- % % continue
- % end
- % Reset Score & Choice Log Data
- score_log = []; % Clear All Score Log Data
- choice_log = []; % Clear All Choice Log Data
- case 2 % User Chose Log Out
- clc; % Clears Command Window
- break; % Exit Logged-in Loop
- case {3,0} % User Chose Quit
- clear;clc; % Clears Command Window & All Variables
- return; % Ends Program
- end
- end
- end
- clear;clc; % Clears the command window and all variables.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement