summaryrefslogtreecommitdiffstats
path: root/Java/BlackJack/Blackjack.java
blob: ff06b7832e68e6596b595aa1d14ab1fe3965b196 (plain) (blame)
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
//TODO

import java.util.Random; //for randomness
//import java.io.BufferedReader;
//import java.io.InputStreamReader;
import java.io.*;

class Blackjack {
	public static void main(String [] args) throws java.io.IOException {
		System.out.println("Welcome To Blackjack!");
		System.out.println("Copyright (C) 2021 msglm.");
		System.out.println("License AGPLv3+: GNU Affero GPL version 3 Only");
		System.out.println("<http://www.gnu.org/licenses/agpl.html>.");
		System.out.println("This is free software: you are free to change and redistribute it.");
		System.out.println("There is NO WARRANTY, to the extent permitted by law. \n");

		//first is for card value, second is for card type
		int[] dealerFaceValues = new int[21];
		int[] dealerSuitValues = new int[21]; 
		int[] playerFaceValues = new int[21];
		int[] playerSuitValues = new int[21];

		int dealerDeltCards = 0;
		int playerDeltCards = 0;

		int dealerScore = 0;
		int playerScore = 0;
		int dealerScoreSuit = 0;
		int playerScoreSuit = 0;

		int roundnum=0;

		boolean stood = false;
		boolean gameEnd = false;
		boolean playerHit = true;
		boolean dealerHit = true;
		Random random = new Random();

		dealerFaceValues[0] = random.nextInt(9+1)+1;
		dealerSuitValues[0] = random.nextInt(1+1)+1;

		playerFaceValues[0] = random.nextInt(9+1)+1;
		playerSuitValues[0] = random.nextInt(1+1)+1;


		//TODO: SWITCH STATEMENT THAT ASSIGNES A NAME TO EACH CARD

		System.out.println("Dealer's Hand:");
		for(int i=0;i<21;i++){
			int curcar = i + 1;
			if(dealerFaceValues[i] != 0 || dealerSuitValues[i] != 0){
				System.out.println("Card " + curcar + " has a Face of " + dealerFaceValues[i] + " and a Suit of " + dealerSuitValues[i]);
			}
		}


		System.out.println("Player's Hand:");
		for(int i=0;i<21;i++){
			int curcar = i + 1;
			if(playerFaceValues[i] != 0 || playerSuitValues[i] != 0){
				System.out.println("Card " + curcar + " has a Face of " + playerFaceValues[i] + " and a Suit of " + playerSuitValues[i]);
			}
		}
		while(!gameEnd){
			roundnum++;
			System.out.println("Would you like a card? Y/n");

			BufferedReader cardWantInput = new BufferedReader(new InputStreamReader(System.in));
			String tempString = cardWantInput.readLine();
			char cardWant = tempString.charAt(0);
                        System.out.println("===ROUND " + roundnum + " ===");
			if(cardWant == 'y' || cardWant == 'Y'){
				playerHit = true;
				playerDeltCards++;
				playerFaceValues[playerDeltCards] = random.nextInt(9+1)+1;
				playerSuitValues[playerDeltCards] = random.nextInt(1+1)+1;
			} else {
				stood = true;
				playerHit = false;
			}
			System.out.println("");
			System.out.println("Dealer's Hand:");
			for(int i=0;i<21;i++){
				int curcar = i + 1;
				if(dealerFaceValues[i] != 0 || dealerSuitValues[i] != 0){
					if(curcar == 1){
						System.out.println("Card " + curcar + " has a Face of " + dealerFaceValues[i] + " and a Suit of " + dealerSuitValues[i]);
					} else {
						System.out.println ("The Dealer has another card...");
					}
				}
			}
			System.out.println("");
			System.out.println("Player's Hand:");
			for(int i=0;i<21;i++){
				int curcar = i + 1;
				if(playerFaceValues[i] != 0 || playerSuitValues[i] != 0){
					System.out.println("Card " + curcar + " has a Face of " + playerFaceValues[i] + " and a Suit of " + playerSuitValues[i]);
				}
			}
			System.out.println("");


			//Find each one's score
			for (int i=0;i<21;i++){
				playerScore = playerScore + playerFaceValues[i];
			}

			for (int i=0;i<21;i++){
				dealerScore = dealerScore + dealerFaceValues[i];
			}

			for (int i=0;i<21;i++){
				playerScoreSuit = playerScoreSuit + playerSuitValues[i];
			}

			for (int i=0;i<21;i++){
				dealerScoreSuit = dealerScoreSuit + dealerSuitValues[i];
			}

			if(dealerScore < 17){
				dealerHit = true;
				dealerDeltCards++;
				dealerFaceValues[dealerDeltCards] = random.nextInt(9+1)+1;
				dealerSuitValues[dealerDeltCards] = random.nextInt(1+1)+1;
			} else {
				dealerHit = false;
			}


			System.out.println("Status:");
			if(playerHit){
			System.out.println("You took a Hit!");
			} else {
			System.out.println("You decided to stand...");
			}


			if(dealerHit){
			System.out.println("The dealer has taken a hit!");
			} else {
			System.out.println("The dealer has decided to stand...");
			}

			if(dealerScore > 21) {
				System.out.println("DEALER BUSTED!!! YOU WIN!");
				gameEnd = true;
			}
			if(playerScore > 21){
				System.out.println("YOU BUSTED!!! YOU LOSE");
				gameEnd = true;

			}

			//probably should clean this up with swich statments. This looks horrible.
			if (stood == true && gameEnd == false) {
				if(dealerScore > playerScore){
					System.out.println("DEALER HAS THE HIGHER CARDS!!! YOU LOSE!");
					gameEnd = true;
				} else if (playerScore > dealerScore){
					System.out.println("YOU HAVE THE HIGHER CARDS!!! YOU WIN!");
					gameEnd = true;
				} else {
					System.out.println("YOU HAVE THE SAME VALUES. WE MUST LOOK AT THE SUITS TO DETERMINE WHO WON");
					if(dealerScore + dealerScoreSuit > playerScore + playerScoreSuit){
						System.out.println("DEALER HAS THE HIGHER CARDS!!! YOU LOSE!");
						gameEnd = true;
					} else if (playerScore + playerScoreSuit > dealerScore + dealerScoreSuit){
						System.out.println("YOU HAVE THE HIGHER CARDS!!! YOU WIN!");
						gameEnd = true;
					} else {
						System.out.println("THE VALUES ARE STILL THE SAME! A COIN FLIP TO DECIDE WHO WON!");
						if(random.nextInt(1) == 1){
							System.out.println("YOU HAVE WON BY PURE LUCK!");
							gameEnd = true;
						} else {
							System.out.println("YOU HAVE LOST BY PURE LUCK!");
							gameEnd = true;
						}}}}
			//reset the score counted for next round
			playerScoreSuit=0;
			dealerScoreSuit=0;
			dealerScore=0;
			playerScore=0;
		}}}