@ajeshjhalli/

NEA Card Game

Python

No description

fork
loading
Files
  • main.py
  • current_deck_name.txt
  • deck_names.txt
  • DefaultDeck.txt
  • leaderboard.txt
  • password.txt
  • round_delay.txt
  • title.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
# Last updated on 20/10/2018

import os
from random import shuffle
from time import sleep
from termcolor import colored

# Card index constants
COLOUR = 0
NUMBER = 1

# Player constants
DRAW = 'draw'
PLAYER1 = '1'
PLAYER2 = '2'

def clear():
	print('\n' * 100)

	
def main():

	clear()
	
	if not login():
		return
	
	game = Game()

	menu_loop = True
	
	menu_array = [
			'                                              ',
			'                                              ',
			'  1 - Play game                               ',
			'  2 - Create a new deck                       ',
			'  3 - Load a deck                             ',
			'  4 - Delete a deck                           ',
			'  5 - Change the speed of the game            ',
			'  6 - View the leaderboard                    ',
			'  7 - Quit                                    ',
			'                                              ',
			'                                              '
	]

	while menu_loop:

		clear()

		with open('title.txt', 'r') as image_file:
			image_text = image_file.read()

		for c in image_text:
			
			if c == '0':
				print(' ', end='')
				
			elif c == '1':
				c = colored(' ', 'yellow', 'on_yellow')
				print(c, end='')
				
			elif c == '=' or c == '|':
				c = colored(' ', 'blue', 'on_blue')
				print(c, end='')
				
			elif c == '\n':
				print()

		print()

		for i in range(len(menu_array)):
			
			line = menu_array[i]
			
			if i == 0 or i == len(menu_array) - 1:
				print(colored(' ' * len(line), 'blue', 'on_blue'))
			else:
				
				first_char = colored(' ', 'blue', 'on_blue')
				middle = colored(line[1:-1], 'cyan')
				last_char = colored(' ', 'blue', 'on_blue')
				print(first_char + middle + last_char)

		menu_option = input('\nEnter menu option: ')

		if menu_option == '1':
			clear()
			game.play()
			input('Press enter to continue.')

		elif menu_option == '2':
			clear()
			new_deck_menu()
			input('Press enter to continue.')

		elif menu_option == '3':
			clear()
			change_deck_menu()
			input('Press enter to continue.')

		elif menu_option == '4':
			clear()
			delete_deck_menu()
			input('Press enter to continue.')

		elif menu_option == '5':
			clear()
			change_speed()
			input('Press enter to continue.')

		elif menu_option == '6':
			clear()
			try:
				top5 = FileFunctions.read_top5()
				display_leaderboard(top5) if len(top5) > 0 else print('\nThere are no players on the leaderboard yet.\n')
			except FileNotFoundError:
				print('\nCould not find leaderboard file.\n')
			input('Press enter to continue.')

		elif menu_option == '7':
			print('\nGoodbye.')
			menu_loop = False
		
		else:
			clear()
			print('\nPlease choose a number from the menu.\n')
			input('Press enter to continue.')


def login():

	try:
		# The password (for now) is 'Python'.
		password = FileFunctions.get_password()
	
	except EOFError:
		print('Could not read the password file. The game cannot be played.')
		return False
		
	except FileNotFoundError:
		print('Could not find the password file. The game cannot be played.')
		return False
	
	valid = False

	while not valid:

		password_attempt = input('Enter password: ')
		clear()

		if password_attempt == password:
			valid = True
		else:
			print('Incorrect password.')
			
	return True


# Returns a tuple containing a colour and a number.
def new_card(colour, number):
	return (colour, number)

# Creates a new random deck.
def new_deck(name, number_of_cards):

	deck = []
	
	for _ in range(int(number_of_cards / 30)):
		for i in range(10):
			deck.append(new_card('red', i+1))
			deck.append(new_card('black', i+1))
			deck.append(new_card('yellow', i+1))

	FileFunctions.write_deck(name, deck)


def display_leaderboard(players):

	length = 20

	print('\nLEADERBOARD\n')
	print('=' * (length + 2))

	for i in range(len(players)):

		score = len(players[i]) - 1

		string_part1 = str(i+1) + ' | ' + players[i][0]
		string_part2 = ' ' * (length - len(string_part1)) + str(score)

		print("%s%s" % (string_part1, string_part2))

	print('=' * (length + 2))
	print('\n')


def change_deck_menu():

	valid = False

	while not valid:

		yes_or_no = input('Loading a different deck will reset the leaderboard. Do you wish to proceed? (y/n)').lower()
		clear()

		if yes_or_no == 'y':
			valid = True
		elif yes_or_no == 'n':
			print('\nDeck has not been loaded.\n')
			return
		else:
			print('\nPlease answer with \'y\' or \'n\'.\n')

	FileFunctions.clear_leaderboard()

	valid = False

	try:
		decks = FileFunctions.load_deck_names()
		decks_found = True
	except FileNotFoundError:
		decks_found = False

	while not valid:

		if decks_found:
			print('\nDecks:\n')
			for deck in decks:
				print(deck)
		else:
			print('\nThe deck names could not be found, but you should still be able to load a deck.\n')

		deck_name = input('\nEnter name of deck to load: ')
		clear()

		if deck_name.strip() == '':
			print('\nThe deck name will contain at least one visible character.')
			continue
			
		try:
			f = open(deck_name + '.txt', 'r')
			f.close()
			valid = True

		except FileNotFoundError:
			print('\nDeck \'%s\' does not exist.' % deck_name)
			valid = False

	FileFunctions.change_current_deck_name(deck_name)

	print('\nDeck \'%s\' has been loaded.\n' % deck_name)


def new_deck_menu():

	print('\n\n')

	valid = False

	while not valid:

		yes_or_no = input('\nAre you sure you want to create a new deck? (y/n)')
		yes_or_no = yes_or_no.lower()
		clear()

		valid = True

		if yes_or_no == 'y':

			name_valid = False

			while not name_valid:

				banned_names = ('password', 'title', 'leaderboard', 'round_delay', 'current_deck')

				deck_name = input('\nEnter deck name: ')
				clear()

				if deck_name.strip() == '':
					print('\nThe deck name must contain at least one visible character.\n')

				elif ' ' in deck_name:
					print('\nThe deck name cannot contain spaces.\n')

				elif '.' in deck_name:
					print('\nThe deck name cannot contain dots (the file extension will be added automatically).\n')

				elif '\\' in deck_name or '/' in deck_name:
					print('\nThe deck name cannot contain slashes.\n')
					
				elif deck_name in banned_names:
					print('\nYour deck name cannot be any of the following:')
					for name in banned_names:
						print(name)
					print()
				
				else:
					name_valid = True

			number_valid = False

			while not number_valid:

				try:
					number = int(input('Enter amount of cards: '))
				except ValueError:
					clear()
					print('\nPlease enter an integer.\n')
					continue
				
				clear()

				if not (number % 2 == 0 and number % 3 == 0):
					print('\nAmount must be an even multiple of 3.\n')
					
				elif number == 0:
					print('\nIf there are no cards in a deck, is it still a deck?\n')
				
				elif number < 0:
					print('\nYou can\'t have a negative amount of cards.\n')
					
				else:
					number_valid = True

			new_deck(deck_name, number)

			print('\nThe new deck has been created.')

		elif yes_or_no == 'n':
			print('\nCreation of new deck has been cancelled.')

		else:
			print('\nPlease answer with \'y\' or \'n\'.')
			valid = False

	print('\n')


def change_speed():

	valid = False

	try:
		current_delay = FileFunctions.load_round_delay()
	except FileNotFoundError:
		print('\nThe round delay file could not be loaded.\n')
		return
		
	current_delay = round(current_delay, 3)
	current_delay = str(current_delay)

	while not valid:

		print('The current round delay is %s seconds.\n' % (current_delay))

		yes_or_no = input('Are you sure you want to change the speed of the game? (y/n)').lower()
		valid = True
		
		clear()

		if yes_or_no == 'y':

			input_loop = True

			while input_loop:

				input_loop = False

				try:
					seconds = float(input('\nEnter delay between each round in seconds: '))
					
					clear()

					if seconds < 0:
						print('The round delay cannot be a negative number.\n')
						input_loop = True
						
				except ValueError:
					input_loop = True
					clear()
					print('Please enter a float or an integer.\n')

			FileFunctions.write_round_delay(seconds)

			print('\nThe new round delay has been saved.\n')

		elif yes_or_no == 'n':
			print('\nChanging of game speed has been cancelled.\n')

		else:
			print('\nPlease answer with \'y\' or \'n\'.\n')
			valid = False


def delete_deck_menu():
	
	valid = False
	
	while not valid:
		
		yes_or_no = input('Are you sure you want to delete a deck? (y/n)').lower()
		clear()
		
		if yes_or_no == 'y':
			valid = True
		elif yes_or_no == 'n':
			print('\nDeletion of deck has been cancelled.\n')
			return
		else:
			print('\nPlease answer with \'y\' or \'n\'.\n')
	
	deck_names = FileFunctions.load_deck_names()

	valid = False

	while not valid:
		
		print('\nDecks:\n')
		for name in deck_names:
			print(name)

		deck_name = input('\nEnter the name of the deck you want to delete: ')

		clear()

		if deck_name.strip() == '':
			print('\nThe deck name will contain at least one visible character.')
		elif ' ' in deck_name:
			print('\nThe deck name will not contain spaces.')
		elif '.' in deck_name:
			print('\nPlease only enter the name of the deck. The file extension will be added automatically.\n')
		else:

			try:
				with open(deck_name + '.txt', 'r'):
					valid = True
					
				os.remove(deck_name + '.txt')
	
				with open('deck_names.txt', 'r') as deck_names_file:
					current_deck_names = deck_names_file.read().split('\n')
			
		
				current_deck_names.remove(deck_name)

			except (FileNotFoundError, ValueError):
				valid = False
				print('\nDeck \'%s\' could not be found. Make sure you have spelt the name correctly.' % deck_name)
	
	
		
	
	with open('deck_names.txt', 'w') as deck_names_file:
		deck_names_file.write('\n'.join(current_deck_names))
		deck_names_file.write('\n')

	print('\nDeck \'%s\' has been deleted.\n' % deck_name)


class Game:

	def __init__(self):

		self.player1_name = ''
		self.player2_name = ''

	def play(self):

		round_delay = FileFunctions.load_round_delay()

		self.player1_name, self.player2_name = self._get_names()

		deck_name = FileFunctions.load_current_deck_name()
		
		if deck_name.strip() == '':
			print('Please load a deck with option of 3 the main menu.')
			return

		play_again = True

		while play_again:

			# Read the deck from the deck file.
			deck = FileFunctions.load_deck()

			player1_cards = []
			player2_cards = []

			shuffle(deck)

			game_round = 1

			print('\n\n')

			while len(deck) > 0:

				sleep(round_delay)

				try:
					player1_card = deck[-1]
					player2_card = deck[-2]
				except IndexError: # The deck is empty
					break

				deck.pop()
				deck.pop()

				print('ROUND', game_round, '\n')

				winner = self._compare_cards(player1_card, player2_card)
				self._display_cards(player1_card, player2_card)

				if winner == PLAYER1:
					print('\nWinner:', self.player1_name)
				elif winner == PLAYER2:
					print('\nWinner:', self.player2_name)
				else:
					print('\nWinner: draw')

				print('\n\n')

				if winner == PLAYER1:
					player1_cards.append(player1_card)
					player1_cards.append(player2_card)

				elif winner == PLAYER2:
					player2_cards.append(player1_card)
					player2_cards.append(player2_card)

				# If it is a draw, the players keep their own cards.
				else:
					player1_cards.append(player1_card)
					player2_cards.append(player2_card)

				game_round += 1

			if len(player1_cards) > len(player2_cards):
				winner = self.player1_name
				winning_cards = player1_cards

			elif len(player1_cards) < len(player2_cards):
				winner = self.player2_name
				winning_cards = player2_cards

			else:
				winner = DRAW
				winning_cards = []


			print('%s has %d cards.' % (self.player1_name, len(player1_cards)))
			print('%s has %d cards.\n' % (self.player2_name, len(player2_cards)))

			print('Winner of game:', winner)

			if winner != DRAW:
				FileFunctions.write_name_and_cards(winner, winning_cards)
				self._display_winning_cards(winner, winning_cards)

			valid = False

			while not valid:
				yes_or_no = input('\nWould you like to play again? (y/n)').lower()
				valid = True
				if yes_or_no == 'n':
					play_again = False
				elif yes_or_no != 'y':
					print('Please answer with \'y\' or \'n\'.')
					valid = False

		print('\n\n')

	def _get_names(self):
		
		print('\n\n')

		valid = False

		while not valid:

			player1_name = input('Enter player 1\'s name: ')

			if '_' in player1_name:
				print('Names cannot contain underscores.')
			elif player1_name.strip() == '':
				print('The name must contain at least one visible character.')
			elif len(player1_name) > 15:
				print('The name cannot contain more than 15 characters.')
			else:
				valid = True

		valid = False

		while not valid:

			player2_name = input('Enter player 2\'s name: ')

			if '_' in player2_name:
				print('Names cannot contain underscores.')
			elif player2_name.strip() == '':
				print('The name must contain at least one visible character.')
			elif player2_name == player1_name:
				print('Player 1 and player 2 must have different names.')
			elif len(player2_name) > 15:
				print('The name cannot contain more than 15 characters.')
			else:
				valid = True

		return (player1_name, player2_name)

	def _display_cards(self, card1, card2):
		
		card_size = 15
		
		p1_bg_colour = card1[COLOUR]
		p2_bg_colour = card2[COLOUR]
		
		if p1_bg_colour == 'black':
			p1_bg_colour = 'grey'
			
		if p2_bg_colour == 'black':
			p2_bg_colour = 'grey'
		
		if p1_bg_colour in ('grey', 'red'):
			p1_fg_colour = 'white'
		elif p1_bg_colour == 'yellow':
			p1_fg_colour = 'grey'
			
		if p2_bg_colour in ('grey', 'red'):
			p2_fg_colour = 'white'
		elif p2_bg_colour == 'yellow':
			p2_fg_colour = 'grey'
		
		p1_bg_colour = 'on_' + p1_bg_colour
		p2_bg_colour = 'on_' + p2_bg_colour
		
		space_between_cards = ' ' * 15
		
		space_length = card_size - len(self.player1_name)
		players_string = colored(self.player1_name.upper(), p1_fg_colour, p1_bg_colour)
		players_string += colored(' ' * space_length, p1_fg_colour, p1_bg_colour)
		players_string += space_between_cards
		players_string += colored(self.player2_name.upper(), p2_fg_colour, p2_bg_colour)
		space_length = card_size - len(self.player2_name)
		players_string += colored(' ' * space_length, p2_fg_colour, p2_bg_colour)

		line = colored('=' * card_size, p1_fg_colour, p1_bg_colour)
		line += space_between_cards
		line += colored('=' * card_size, p2_fg_colour, p2_bg_colour)
		
		print(line)
		print(players_string)
		print(line)
		
		p1_number_array = self._get_number_string(card1[NUMBER]).split('\n')
		p2_number_array = self._get_number_string(card2[NUMBER]).split('\n')
		
		length = len(p2_number_array) if len(p2_number_array) > len(p1_number_array) else len(p1_number_array)
		
		for i in range(length):
			
			output_line = ''
			p1_line = p1_number_array[i]
			
			for j in range(len(p1_line)):
				if p1_line[j] == '1':
					output_line += colored(' ', p1_fg_colour, 'on_' + p1_fg_colour)
				else:
					output_line += colored(' ', p1_fg_colour, p1_bg_colour)
					
			space_length = card_size - len(p1_line)
			output_line += colored(' ' * space_length, p1_fg_colour, p1_bg_colour)
			output_line += space_between_cards
					
			p2_line = p2_number_array[i]
					
			for j in range(len(p2_line)):
				if p2_line[j] == '1':
					output_line += colored(' ', p2_fg_colour, 'on_' + p2_fg_colour)
				else:
					output_line += colored(' ', p2_fg_colour, p2_bg_colour)
					
			space_length = card_size - len(p2_line)
			output_line += colored(' ' * space_length, p2_fg_colour, p2_bg_colour)
					
			print(output_line)


	def _compare_cards(self, card1, card2):

		if card1[COLOUR] == card2[COLOUR]:

			if card1[NUMBER] > card2[NUMBER]:
				return PLAYER1
			elif card1[NUMBER] < card2[NUMBER]:
				return PLAYER2
			else:
				return DRAW
		
		else:

			if card1[COLOUR] == 'red':
				return PLAYER1 if card2[COLOUR] == 'black' else PLAYER2
			elif card1[COLOUR] == 'black':
				return PLAYER1 if card2[COLOUR] == 'yellow' else PLAYER2
			elif card1[COLOUR] == 'yellow':
				return PLAYER1 if card2[COLOUR] == 'red' else PLAYER2

	def _display_winning_cards(self, winner, winning_cards):

		while winner[-1] == ' ':
			winner = winner[:-1]

		if winner[-1].lower() == 's':
			winner += '\''
		else:
			winner += '\'s'

		print('\n%s CARDS:\n' % winner.upper())
		
		space_const = 15
		space_after_colour = ''
		length_of_largest_int = len( str( len(winning_cards) + 1 ) )

		for i in range(len(winning_cards)):

			card = winning_cards[i]

			space_after_colour = ' ' * ( space_const - len(card[0]) )
			space_after_number = length_of_largest_int - len(str(i+1))

			card_string = str(i+1)
			card_string += ' ' * space_after_number
			card_string += ' | COLOUR: ' + card[0] + space_after_colour + 'NUMBER: ' + str(card[1])

			print(card_string)
			
	def _get_number_string(self, number):
		
		if number > 10 or number < 0:
			print('Invalid card', end=' ')
		
		number_strings = (
			
			'   1\n   1\n   1\n   1\n   1',
			'1111\n   1\n1111\n1   \n1111',
			'1111\n   1\n1111\n   1\n1111',
			'1  1\n1  1\n1111\n   1\n   1',
			'1111\n1   \n1111\n   1\n1111',
			'1111\n1   \n1111\n1  1\n1111',
			'1111\n   1\n   1\n   1\n   1',
			'1111\n1  1\n1111\n1  1\n1111',
			'1111\n1  1\n1111\n   1\n   1',
			'1  1111\n1  1  1\n1  1  1\n1  1  1\n1  1111'
		)
		
		return number_strings[number - 1]
		
		
class FileFunctions:

	# Reads the current deck's name
	def load_current_deck_name():
		with open('current_deck_name.txt', 'r') as cd_file:
			return cd_file.read()

	def change_current_deck_name(new_name):
		with open('current_deck_name.txt', 'w') as cd_file:
			cd_file.write(new_name)

	# Writes deck to a file
	def write_deck(name, deck_array):

		deck_string = ''

		for card in deck_array:

			deck_string += card[0]
			deck_string += ','
			deck_string += str(card[1])
			deck_string += '\n'

		with open(name + '.txt', 'w') as deck_file:
			deck_file.write(deck_string)
			
		with open('deck_names.txt', 'a') as deck_names_file:
			deck_names_file.write(name + '\n')

	# Reads deck from a file and returns it as an array
	def load_deck():
		
		with open('current_deck_name.txt', 'r') as current_deck_name_file:
			name = current_deck_name_file.read()

		deck_array = []

		with open(name + '.txt', 'r') as deck_file:
			deck_text = deck_file.read()

		deck_text = deck_text.split('\n')

		for card_string in deck_text:

			try:
				card = card_string.split(',')
				card[1] = int(card[1])
				deck_array.append( (card[0], card[1]) )
			except IndexError: # The line is empty
				continue

		return deck_array

	def get_password():
		with open('password.txt', 'r') as password_file:
			return password_file.read()

	def clear_leaderboard():
		with open('leaderboard.txt', 'w'):
			pass

	# Writes name and cards to win.bin
	def write_name_and_cards(name, cards):

		# Write the name and cards to the file
		
		try:
			with open('leaderboard.txt', 'r') as win_file:
				win_string = win_file.read()
		except EOFError:
			win_string = ''

		win_string += name

		for card in cards:
			win_string += '\n'
			win_string += card[0]
			win_string += ','
			win_string += str(card[1])

		win_string += '_'

		with open('leaderboard.txt', 'w') as win_file:
			win_file.write(win_string)

		# Delete any players not in the top 5

		# Read all players from file.
		with open('leaderboard.txt', 'r') as win_file:
			players_string = win_file.read()

		# Convert the string into an array.
		players = players_string.split('_')

		# Convert the array into a 2D array.
		for i in range(len(players)):
			players[i] = players[i].split('\n')

		# Remove ['']
		while players[-1] == ['']:
			players.pop()

		top5 = []

		while len(top5) < 5:

			index_of_highest = 0

			for i in range(len(players)):
				if len(players[i]) > len(players[index_of_highest]):
					index_of_highest = i
			try:
				top5.append(players[index_of_highest])
				players.pop(index_of_highest)
			except IndexError:
				break # The players array contains less than 5 players.

		top5_string = ''

		for player in top5:
			top5_string += '\n'.join(player)
			top5_string += '_'

		with open('leaderboard.txt', 'w') as win_file:
			win_file.write(top5_string)

	# Returns the top 5 players from win.txt as a tuple
	def read_top5():

		with open('leaderboard.txt', 'r') as win_file:
			players = win_file.read()

		players = players.split('_')

		for i in range(len(players)):
			players[i] = players[i].split('\n')

		try:
			while players[-1] == ['']:
				players.pop()
		except IndexError: # The players array might be empty
			pass

		return players

	def write_round_delay(seconds):
		with open('round_delay.txt', 'w') as rd_file:
			rd_file.write(str(seconds))

	def load_round_delay():
		with open('round_delay.txt', 'r') as rd_file:
			return float(rd_file.read())
			
	def load_deck_names():
		
		with open('deck_names.txt', 'r') as deck_names_file:
			deck_names_string = deck_names_file.read()
			
		deck_names = deck_names_string.split('\n')
		
		return deck_names
		

if __name__ == '__main__':
	main()