· 8 years ago · May 12, 2018, 08:48 PM
1% Reseting Environment & Muting Erroneous Error Notifications
2clear;clc; % Clears the command window and all variables.
3%#ok<*SAGROW>
4%#ok<*AGROW>
5%#ok<*NOPTS>
6
7% Log
8if exist('Log.txt','file') == 2
9 log_old = csvread('Log.txt'); log_new = clock;
10 log = [log_old;log_new];
11 csvwrite('Log.txt',log);
12else
13 csvwrite('Log.txt',clock);
14end
15
16% Choice Variable Values
17rock = 1;
18paper = 2;
19scissors = 3;
20
21% Outcome/Player Identification Values Assigned
22user_n = 1;
23comp_n = 2;
24draw_n = 3;
25none_n = 0;
26
27% Commonly Used Strings Assigned to Variables
28markers = {'start';'end'};
29markerz = {0;0};
30end_Cell = {0,0,0,0,0};
31
32main = 0;
33while main == 0 % Loop Allowing User to Return to Login/Register Menu (From Later Points in Program)
34
35 % Clear Variables from Last Iteration
36 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)
37
38 % Check for File with User Data
39 if exist('Users.txt','file') == 2 % File 'Users.txt' (File with User Information) exists
40 % continue
41 else % File 'Users.txt' doesn't exist
42 writetable(table(markers,markers,markers,'VariableNames',{'User';'Pass';'Role'}),'Users.txt'); % Create File 'Users.txt' to Store User Login Data
43 mkdir('RSP/','Users'); % Create Directory 'Users' to Store User History Data Files
44 end
45
46 % Login/Register Loop
47 login = 0;
48 while login == 0 % Loop Allowing User to Return to Login/Register Menu (From Login/Register Point in Program)
49 read_Users = readtable('Users.txt','ReadVariableNames',true); % Gather All User Login Data for Later Use
50 login_menu = menu('Login Menu','Registered User','New User','Quit'); % Prompt User to Login, Create User, or Quit Program
51 switch login_menu % User Login/Register Menu Choice
52 case 1 % User Chose Registered User
53 creds{1,1} = input('Input Username: ','s'); % User Inputs Supposed Registered Username
54 creds_counter = 0; % Counter Recording Number of Rows Checked for User Input Username is Created/Reset
55 valid_user = 0;
56 while valid_user == 0 % Loop Continues Until User Input Username is Deemed Valid or Invalid
57 creds_counter = creds_counter + 1; % Counter Increases to Account for Checking of an Additional Row
58 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)
59 valid_user = 2; % User Input Username Marked Invalid (No Username May Match Indicator Strings)
60 elseif strcmp(char(read_Users.User(creds_counter)),creds{1,1}) % If User Input Username Matches Stored User Login Data (Username is Registered)
61 valid_user = 1; % User Input Username Marked Valid
62 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)
63 valid_user = 2; % User Input Username Marked Invalid
64 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
65 % continue
66 end
67 end
68 switch valid_user % User Input Username Validity
69 case 1 % User Input Username is Valid
70 valid_pass = 0;
71 while valid_pass == 0 % Loop Continues Until User Inputs Correct Password (For Input Username User) or Stops Trying After Incorrect Input
72 creds{1,2} = input('Input Password: ','s'); % User Inputs Supposed Correct Password (For Input Username User)
73 clc; % Clears Command Window
74 if strcmp(char(read_Users.Pass(creds_counter)),creds{1,2}) % If User Input Password Matches Recorded Password for Input Username User
75 fprintf('Welcome, %s.',creds{1,1}); % Display Personalized Welcome Message (Featuring Unique Username)
76 valid_pass = 1; % User Input Password Marked Valid
77 login = 1; % Condition for Login is Marked as Fulfilled Allowing Program to Continue (Past the Login/Register Section)
78 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
79 pass_menu = menu('Incorrect Password','Try Again','Main Menu'); % User Can Attempt to Input Correct Password (For Input Username User) Again or Stop Trying
80 switch pass_menu % User Invalid Password Menu Choice
81 case 1 % User Chose Try Again
82 fprintf('Input Username: %s\n',creds{1,1}); % Input Username is Displayed to Mirror Previous Display
83 case {2,0} % User Chose to Stop Trying
84 valid_pass = 2; % User Input Password Marked Invalid
85 end
86 end
87 end
88 case 2 % User Input Username is Invalid
89 clc; % Clears Command Window
90 fprintf('Error: User ''%s'' does not exist.',creds{1,1}); % Display Invalid Username Error Message
91 end
92 case 2 % User Chose New User
93 create_user = 0;
94 while create_user == 0 % Loop Continues Until User Successfully Creates New User or Stops Trying After Invalid Input
95 clc; % Clears Command Window
96 fprintf('*Usernames/Passwords are case sensative and cannot be blank*\n\n'); % Display Parameters for User Input New Username and Password
97 creds{1,1} = input('Create Username: ','s'); % User Inputs Supposed New Username Supposedly Within Previously Displayed (& Still Visible) Parameters
98 creds_counter = 0; % Counter Recording Number of Rows Checked for User Input New Username is Created/Reset
99 avail_user = 0;
100 while avail_user == 0 % Loop Continues Until User Input New Username is Deemed Available or Unavailable
101 creds_counter = creds_counter + 1; % Counter Increases to Account for Checking of an Additional Row
102 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)
103 avail_user = 2; % User Input New Username Marked Unavailable (No Username May Match Indicator Strings)
104 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)
105 avail_user = 1; % User Input New Username Marked Available
106 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)
107 avail_user = 2; % User Input New Username Marked Unavailable (No Username May Match Another)
108 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
109 % continue
110 end
111 end
112 switch avail_user % User Input New Username Availability
113 case 1 % User Input New Username Marked Available
114 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
115 for creds_c = 1:3 % For All Columns in the New User Data Cell
116 creds{2,creds_c} = 'end'; % Indicator String (Indicating Last Row in Table/Cell/File) Appended to New User Data Cell
117 end
118 read_Users = table2cell(read_Users); % Convert User Login Data Table to Cell to Enable Editing
119 read_Users(size(read_Users,1),:) = []; % Delete Bottom Row of Cell to Enable Seamless Data Appending
120 read_Users = [read_Users;creds]; % Append New Data (For New User) to User Login Data
121 read_Users = cell2table(read_Users,'VariableNames',{'User';'Pass';'Role'}); % Convert User Login Data Cell to Table for File Writing
122 writetable(read_Users,'Users.txt'); % Write User Login Data to User Login Data File
123 hist_initial = table(markerz,markerz,markerz,markerz,markerz,'VariableNames',{'User';'Comp';'Result';'Diff';'Count'}); % Create Table Formatted for Storage of New User History
124 writetable(hist_initial,strcat('RSP/Users/',creds{1,1},'.txt')); % Create Unique File for New User History & Write Formatted Table to File
125 created_menu = menu('User Registered','Main Menu'); % User Informed of Successful New User Registration & Given Option to Return to Login/Register (Main) Menu
126 switch created_menu % User Registration Confirmation Menu Choice
127 case {1,0} % User Chose Login/Register (Main) Menu or Closed Menu
128 clc; % Clears Command Window
129 create_user = 1; % New User Registration Process Marked Complete & Successful (Condition is Met to End New User Registration Loop)
130 end
131 case 2 % User Input New Username Marked Unavailable
132 clc; % Clears Command Window
133 fprintf('Error: Username ''%s'' is unavailable.',creds{1,1}); % Display Unavailable Username Error Message
134 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)
135 switch create_menu % User Unavailable Username Menu Choice
136 case 1 % User Chose Try Again
137 % continue
138 case {2,0} % User Chose Login/Register (Main) Menu or Closed Menu
139 create_user = 2; % New User Registration Process Marked Complete & Unsuccessful (Condition is Met to End New User Registration Loop)
140 end
141 clc; % Clears Command Window
142 end
143 end
144 case {3,0} % User Chose Quit or Closed Menu
145 clear;clc; % Clears Command Window & All Variables
146 return; % Ends Program
147 end
148 end
149
150 loggedin = 1;
151 while loggedin == 1
152 counter = 0; % Reset Round Counter
153 user_menu = menu(creds{1,1},'Play','Log Out','Quit'); % User Can Play, Log Out, or Quit Program
154 switch user_menu % User Menu Selection
155 case 1 % User Chose Play
156
157 % Difficulty Level Selection
158 diff = menu('Choose a difficulty level.','Normal (Random)','Realistic (Human-like)','Impossible (Cold & Calculated)'); % User Selects Difficulty Level
159
160 % Loop for Continuous Play
161 play = 1;
162 while play == 1 % Loop Allowing User to Play Round After Round
163
164 % Preparation for New Round
165 read_Hist = readtable(strcat(creds{1,1},'.txt'),'ReadVariableNames',true); % User History Data is Gathered from Unique User File
166 winner = none_n; % Winner Reset to None
167 counter = counter + 1; % Counter Increased to Represent New Round Number
168 for k = user_n:draw_n % For All Possible Winners
169 score_log(counter,k) = 0; % Row for New Round Data is Appended to Score Log
170 end
171 for k = rock:scissors % For All Possible Player Choices
172 choice_log(counter,k) = 0; % Row for New Round Data is Appended to Choice Log
173 end
174
175 % User Chooses Option
176 user = menu('Rock, paper, or scissors?','ROCK','PAPER','SCISSORS','QUIT'); % User Can Select a Game Choice or Quit Game
177 ucn = user; % User Choice is Recorded
178 switch user % User Choice
179 case rock % User Chose ROCK
180 uc = 'ROCK'; % Choice String Assigned for Later Display
181 case paper % User Chose PAPER
182 uc = 'PAPER';
183 case scissors % User Chose SCISSORS
184 uc = 'SCISSORS';
185 case {4,0} % User Chose to Quit Program
186 clc; % Clear Command Window
187 break; % Exit Play Loop
188 end
189 clc; % Clears Command Window
190 fprintf('-------- %d --------- \n\n',counter); % Display Round Number
191 fprintf('User chose '); % Display Label for User Choice
192 disp(uc); % Display User Choice
193 fprintf('\n'); % Move Cursor to Next Line
194
195 % Computer Chooses Option
196 switch diff % User Difficulty Choice
197 case 1 % User Chose Random
198 comp = randi(3); % Psuedo-Random Number from 1-3 Indicating Comp Choice
199 case 2 % User Chose Realistic
200 comp = RSP_StrategyDA(counter,table2cell(read_Hist)); % Function with Strategy Outputs Comp Choice
201 case 3 % User Chose Impossible
202 switch ucn % User Choice
203 case rock % User Chose ROCK
204 comp = paper; % Comp Chooses PAPER
205 case paper % User Chose PAPER
206 comp = scissors; % Comp Chooses SCISSORS
207 case scissors % User Chose SCISSORS
208 comp = rock; % Comp Chooses ROCK
209 end
210 end
211 ccn = comp; % Comp Choice is Recorded
212 switch comp % Comp Choice
213 case rock % Comp Chose ROCK
214 cc = 'ROCK'; % Choice String Assigned for Later Display
215 case paper % Comp Chose PAPER
216 cc = 'PAPER';
217 case scissors % Comp Chose SCISSORS
218 cc = 'SCISSORS';
219 end
220 fprintf('Computer chose '); % Display Label for Comp Choice
221 disp(cc); % Display Comp Choice
222 fprintf('\n'); % Move Cursor to Next Line
223
224 % Determination of Winner
225 switch user % User Choice
226 case rock % User Chose ROCK
227 switch comp % Comp Choice
228 case rock % Comp Chose ROCK (DRAW)
229 winner = draw_n; % Draw
230 case paper % Comp Chose PAPER (COMP)
231 winner = comp_n; % Comp Won
232 case scissors % Comp Chose SCISSORS (USER)
233 winner = user_n; % User Won
234 end
235 case paper % User Chose PAPER
236 switch comp % Comp Choice
237 case rock % Comp Chose ROCK (USER)
238 winner = user_n;
239 case paper % Comp Chose PAPER (DRAW)
240 winner = draw_n;
241 case scissors % Comp Chose SCISSORS (COMP)
242 winner = comp_n;
243 end
244 case scissors % User Chose SCISSORS
245 switch comp % Comp Choice
246 case rock % Comp Chose ROCK (COMP)
247 winner = comp_n;
248 case paper % Comp Chose PAPER (USER)
249 winner = user_n;
250 case scissors % Comp Chose SCISSORS (DRAW)
251 winner = draw_n;
252 end
253 end
254
255 % Display of Winner
256 switch winner
257 case user_n % USER Won
258 fprintf(' USER WINS \n\n'); % Display User Won
259 case comp_n % COMP Won
260 fprintf(' COMPUTER WINS \n\n'); % Display Comp Won
261 case draw_n % DRAW
262 fprintf(' DRAW \n\n'); % Display Draw
263 end
264
265 % Recording Score for Round
266 score_log(counter,winner) = 1; % Record Round Winner
267
268 % Recording Player Choices for Round
269 choice_log(counter,user_n) = ucn; % Record User Choice
270 choice_log(counter,comp_n) = ccn; % Record Comp Choice
271
272 % Recording User History for Round
273 hist_log = {choice_log(counter,1),choice_log(counter,2),winner,diff,counter}; % User Data for Current Round is Recorded
274 read_Hist = table2cell(read_Hist); % Convert User History Table to Cell to Enable Editing
275 read_Hist(size(read_Hist,1),:) = []; % Delete Bottom Row of Cell to Enable Seamless Data Appending
276 read_Hist = [read_Hist;hist_log;end_Cell]; % Append New Data (For This Round) to User History
277 read_Hist = cell2table(read_Hist,'VariableNames',{'User';'Comp';'Result';'Diff';'Count'}); % Convert User History Cell to Table for File Writing
278 writetable(read_Hist,strcat(creds{1,1},'.txt')); % Write User History to User's File
279
280 % Calculating Total Score of All Rounds
281 for j = user_n:draw_n % For All Possible Winners
282 score_total(1,j) = sum(score_log(:,j)); % Sum of Winner's Wins
283 end
284
285 % Displaying Score to User
286 fprintf('------ Score ------- \n'); % Display Score Header
287 fprintf('User Computer Draw \n'); % Display Score Field Labels
288 fprintf('---- -------- ---- \n'); % Display Line Separating Labels/Header from Data
289 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
290
291 end
292
293 % Available Only to Admins (Users Marked Admins in User File)
294 % if ((counter - 1) > 0) && (strcmp(char(read_Users.Role(creds_counter)),'Admin')) % User is Admin
295 % % Optional Display of Records
296 % 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
297 % switch records % User Record Menu Choice
298 % case {1,2,3} % User Chose to Display Log(s)
299 % fprintf('LEGEND: \n\n Column 1 - User \n Column 2 - Computer \n Column 3 - Draw \n'); % Display Legend to Enable Data Comprehension by User
300 % switch records % User Record Choice
301 % case 1 % Display SCORE & CHOICE Logs
302 % score_log; % Display Score Log
303 % choice_log; % Display Choice Log
304 % case 2 % Display SCORE Log
305 % score_log;
306 % case 3 % Display CHOICE Log
307 % choice_log;
308 % end
309 % case {4,0} % User Chose Not to Display Log(s)
310 % % continue
311 % end
312 % else % User isn't Admin
313 % % continue
314 % end
315
316 % Reset Score & Choice Log Data
317 score_log = []; % Clear All Score Log Data
318 choice_log = []; % Clear All Choice Log Data
319
320 case 2 % User Chose Log Out
321 clc; % Clears Command Window
322 break; % Exit Logged-in Loop
323 case {3,0} % User Chose Quit
324 clear;clc; % Clears Command Window & All Variables
325 return; % Ends Program
326 end
327 end
328end
329
330clear;clc; % Clears the command window and all variables.