Solitaire Java Program - java

Hey guys so I'm taking my first Java class ever and am stuck with an error code while trying to compile this program. I attached both classes hoping somewhere he can help me find the error. This is the error I'm receiving:
Error: constructor Card in class Card cannot be applied to given types;
required: char,char
found: no arguments
reason: actual and formal argument lists differ in length
It shows the error is on the line "Card temp= new Card();" under the public void shuffle method. Any help will be greatly appreciated.
import java.util.Random;
import java.util.Scanner;
public class Deck {
private Card [] data;
public Deck()
{
String suits = "HDSC";
String ranks = "A23456789TJQK";
data = new Card[52];
int count = 0;
Card C1;
for (int s = 0; s < suits.length(); s++){
for (int r = 0; r < ranks.length(); r ++)
{
C1 = new Card(ranks.charAt(r), suits.charAt(s));
data[count++] = C1;
}
}
}
//This function display's the whole deck of cards
// Our output should be as below
// AH 2H 3H ... KH
// AS 2H 3S ... KS
// AD 2D 3D ... KD
// AC 2C 3C ... KC
public void display()
{
int index=1;
for (int i=0; i<52; i++)
{
System.out.print(data[i].rank +"" + data[i].suit + " ");
if (index%13 == 0 && i!=0 )
System.out.println();
index++;
}
}
//This function randomly shuffles the deck of cards
public void shuffle()
{
int index;
Random random = new Random();
for (int i = 0; i<52; i++)
{
index = i + random.nextInt(52 - i);
Card temp= new Card();
if (index != i)
{
temp = data[i];
data[i] = data[index];
data[index] = temp;
}
}
System.out.println("Card Shuffled");
}
public void deal()
{
int sum = 0;
int countPrime = 0;
boolean isPrime = false;
for(int i=0; i<52; i++)
{
sum += data[i].getValue(data[i]);
isPrime = checkPrime(sum);
if (isPrime == true)
{
sum = 0;
countPrime++;
if (i==51)
{
System.out.println("Winner in " + countPrime + " Piles");
break;
}
}
if (i==51)
System.out.println("Loser");
}
}
boolean checkPrime(int num)
{
boolean isPrime = true;
for(int j = 2; j <= num/2; ++j)
{
// condition for nonprime number
if(num % j == 0)
{
isPrime = false;
break;
}
}
return isPrime;
}
//Display menu
public static int menu() {
System.out.println("\nWelcome to Solitaire Prime!");
System.out.println("1) New Deck");
System.out.println("2) Display Deck");
System.out.println("3) Shuffle Deck");
System.out.println("4) Play Solitaire Prime");
System.out.println("5) Exit");
Scanner in=new Scanner(System.in);
int choice = in.nextInt();
return choice;
}
public static void main(String args[])
{
Deck newDeck = null;
int choice;
do{
//Accepts user input for menu
choice = menu();
if (choice == 1)
{
newDeck = new Deck();
System.out.println("New deck created");
}
else if (choice == 2 )
{
newDeck.display();
}
else if (choice == 3 )
{
newDeck.shuffle();
}
else if (choice == 4 )
{
newDeck.shuffle();
newDeck.deal();
}
else if (choice == 5)
{
System.out.println("Exiting game. Goodbye!");
return;
}
else
System.out.println("Wrong choice! Please try again.");
}while(choice!=5);
}
}
public class Card {
char suit;
char rank;
public Card(char r, char s)
{
rank = r;
suit = s;
}
public void menu()
{
}
public void display(Card C1)
{
char suit = getSuit(C1);
char rank = getRank(C1);
String suitName = "";
String rankName = "";
if (suit == 'S')
suitName = "Spade";
else if (suit == 'H')
suitName = "Hearts";
else if (suit == 'C')
suitName = "Clubs";
else if (suit == 'D')
suitName = "Diamonds";
if (rank == 'A')
rankName = "Ace";
else if (rank == '2')
rankName = "Two";
else if (rank == '3')
rankName = "Three";
else if (rank == '4')
rankName = "Four";
else if (rank == '5')
rankName = "Five";
else if (rank == '6')
rankName = "Six";
else if (rank == '7')
rankName = "Seven";
else if (rank == '8')
rankName = "Eight";
else if (rank == '9')
rankName = "Nine";
else if (rank == '1')
rankName = "Ten";
else if (rank == 'J')
rankName = "Jack";
else if (rank == 'Q')
rankName = "Queen";
else if (rank == 'K')
rankName = "King";
System.out.println(rankName + " of " + suitName);
}
//This method gives the value of a card
public int getValue(Card C1)
{
int value = 0;
if (C1.rank == 'A')
value = 1;
else if (C1.rank == '2')
value = 2;
else if (C1.rank == '3')
value = 3;
else if (C1.rank == '4')
value = 4;
else if (C1.rank == '5')
value = 5;
else if (C1.rank == '6')
value = 6;
else if (C1.rank == '7')
value = 7;
else if (C1.rank == '8')
value = 8;
else if (C1.rank == '9')
value = 9;
else if (C1.rank == '1')
value = 10;
else if (C1.rank == 'J')
value = 10;
else if (C1.rank == 'Q')
value = 10;
else if (C1.rank == 'K')
value = 10;
return value;
}
//This method gives the rank of a card
public char getRank(Card C1)
{
return C1.rank;
}
//This method gives the suit of a card
public char getSuit(Card C1)
{
return C1.suit;
}
}

Your Card class only has a constructor that takes two char arguments; there is no zero-argument constructor defined.
When shuffling, you don't need to create a new instance of Card like you're currently doing here: Card temp= new Card(); Instead of creating a new instance of Card and throwing it away, just assign the shuffled Card value to your temp variable like this:
for (int i = 0; i<52; i++)
{
index = i + random.nextInt(52 - i);
if (index != i)
{
Card temp = data[i];
data[i] = data[index];
data[index] = temp;
}
}
Note that we moved temp into the if-block, since it's only used in that scope.

Related

Tic Tac Toe in Java, trying to figure out how to reset the program

I have been trying to figure out how to write the code to reset my program/ clear the board so tic tac toe can be played again. It is supposed to have a prompt that asks "do you want to play again" after a win/tie. It's the final part I am trying to figure out.
Board:
public class Board {
private char[][] board;
public Board() {
char[][] temp = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
board = temp;
}
public void printBoard() {
for (char[] row : board) {
for (char cell : row) {
System.out.printf("| %c ", cell);
}
System.out.println();
}
}
public boolean isCellAvailable(int number) {
if (1 <= number && number <= 9) {
int row = (number - 1) / 3;
int col = (number - 1) % 3;
if (board[row][col] == 'X' || board[row][col] == 'O') return false;
else return true;
}
return false;
}
public void place(int number, char marker) {
int row = (number - 1) / 3;
int col = (number - 1) % 3;
board[row][col] = marker;
}
public boolean isWinner() {
if (board[0][0] == board[0][1] && board[0][1] == board[0][2]) return true;
else if (board[1][0] == board[1][1] && board[1][1] == board[1][2]) return true;
else if (board[2][0] == board[2][1] && board[2][1] == board[2][2]) return true;
else if (board[0][0] == board[1][0] && board[1][0] == board[2][0]) return true;
else if (board[0][1] == board[1][1] && board[1][1] == board[2][1]) return true;
else if (board[0][2] == board[1][2] && board[1][2] == board[2][2]) return true;
else if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return true;
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return true;
return false;
}
}
Driver:
import java.util.Scanner;
class Driver {
public static void main(String[] args) {
Board board = new Board();
Scanner scanner = new Scanner(System.in);
board.printBoard();
int moves = 0;
while (true) {
while (true) {
System.out.print("Player 1: Enter your move: ");
int cell = scanner.nextInt();
if (board.isCellAvailable(cell)) {
board.place(cell, 'X');
board.printBoard();
moves += 1;
break;
} else {
System.out.println("Cell not available.");
}
}
if (board.isWinner()) {
System.out.println("Player 1 wins.");
break;
}
if (moves == 9) {
System.out.println("Draw. Game ended.");
break;
}
while (true) {
System.out.print("Player 2: Enter your move: ");
int cell = scanner.nextInt();
if (board.isCellAvailable(cell)) {
board.place(cell, 'O');
board.printBoard();
moves += 1;
break;
} else {
System.out.println("Cell not available.");
}
}
if (board.isWinner()) {
System.out.println("Player 2 wins.");
break;
}
}
}
}
You need another loop in order to manage the Play again option. If the player wants to play again, a new Board() is created and the moves are reseted as well. Something like this:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
Board board = new Board();
board.printBoard();
int moves = 0;
while(true){
while (true) {
System.out.print("Player 1: Enter your move: ");
int cell = scanner.nextInt();
if (board.isCellAvailable(cell)) {
board.place(cell, 'X');
board.printBoard();
moves += 1;
break;
} else {
System.out.println("Cell not available.");
}
}
if (board.isWinner()) {
System.out.println("Player 1 wins.");
break;
}
if (moves == 9) {
System.out.println("Draw. Game ended.");
break;
}
while (true) {
System.out.print("Player 2: Enter your move: ");
int cell = scanner.nextInt();
if (board.isCellAvailable(cell)) {
board.place(cell, 'O');
board.printBoard();
moves += 1;
break;
} else {
System.out.println("Cell not available.");
}
}
if (board.isWinner()) {
System.out.println("Player 2 wins.");
break;
}
}
System.out.println("Do you want to play again? Press 1, otherwise press 0")
int option = scanner.nextInt();
if(option == 0) break;
}
}

when i use recursive function it returns unexpected result

i've been trying to impelement rock,paper,scissor game in java.
when i try to use getinput method : the first try returns right output 1 , 2 or 3(rock,paper,scissor declared static final in gamelogic class ..)
but when i enter incorrect input then correct input it always returns 0 !
public int getInput(){
System.out.println("Select ROCK , PAPER or SCISSOR");
String choice = scanner.nextLine();
choice = choice.toUpperCase();
char c = choice.charAt(0);
if(c == 'R'){
return gameLogic.rock;
}else if(c == 'P'){
return gameLogic.paper;
}else if(c == 'C'){
return gameLogic.scissor;
}
getInput();
return 0;
}
try this
public static int getInput(){
int result = 0;
System.out.println("Select ROCK , PAPER or SCISSOR");
Scanner scanner = new Scanner(System.in);
String choice = scanner.nextLine();
choice = choice.toUpperCase();
char c = choice.charAt(0);
if(c == 'R'){
result = 1;
}else if(c == 'P'){
result = 2;
}else if(c == 'C'){
result = 3;
} else {
return getInput();
}
return result;
}

Why does my simple poker program continue to draw the same cards?

I'm learning Java for my own and i have this problem:
I created a poker program, and every time, both the AI and I draw nothing but the Ace of Spades (5 each) every round. How can I fix this?
import java.io.IOException;
import java.util.Scanner;
public class poker {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws IOException {
int score1 = 0,score2 = 0;
for (int i=0; i<10; i++) {
int[][] previous_cards = new int[0][2];
int[][] hand1 = generate_hand(previous_cards);
int[][] sorted1 = sort_hand(hand1);
System.out.println("Round "+(i+1)+": ");
System.out.print(" Your hand: ");
print_hand(sorted1);
int identify_hand1 = identify_hand(sorted1);
System.out.print(" ");
print_identify_hand(identify_hand1);
System.out.println();
int[][] hand2 = generate_hand(hand1);
int[][] sorted2 = sort_hand(hand2);
System.out.print(" Computer hand: ");
print_hand(sorted2);
int identify_hand2 = identify_hand(sorted2);
System.out.print(" ");
print_identify_hand(identify_hand2);
System.out.println();
int compared = compare_hands(sorted1,sorted2);
if (compared==-1)
System.out.println(" You win this round!");
else if (compared==1)
System.out.println(" The computer wins this round!");
else System.out.println("Draw!");
score1 += (compared<0)?1:0;
score2 += (compared>0)?1:0;
System.out.println(" Score: You:"+score1+" - Computer:"+score2);
input.nextLine();
}
if (score1<score2)
System.out.println("The computer won with: "+score2+"-"+score1+".");
else if (score1==score2)
System.out.println("Draw: "+score1+"-"+score2+".");
else System.out.println("You won with: "+score1+"-"+score2+".");
}
public static int[][] generate_hand(int[][] previous_cards) {
int[][] hand = new int[5][2];
return hand;
}
public static int[] generate_card() {
int[] card = new int[2];
card[0] = (int) (Math.random()*13 + 2);
card[1] = (int) (Math.random()*4 + 1);
return card;
}
public static int compare_2_cards(int[] card1, int[] card2) {
return 0;
}
public static void print_hand(int[][] hand) {
System.out.print(card_to_String(hand[0])+", ");
System.out.print(card_to_String(hand[1])+", ");
System.out.print(card_to_String(hand[2])+", ");
System.out.print(card_to_String(hand[3])+", ");
System.out.print(card_to_String(hand[4]));
}
public static String card_to_String(int[] c) {
String card = "";
if (2<=c[0] && c[0]<=10)
card += c[0];
else if (c[0]==11) card += "Jack";
else if (c[0]==12) card += "Queen";
else if (c[0]==13) card += "king";
else card += "Ace";
card += " of ";
if (c[1]==1) card += "hearts";
else if (c[1]==2) card += "diamonds";
else if (c[1]==2) card += "clubs";
else card += "spades";
return card;
}
public static int[][] sort_hand(int[][] hand) {
int[][] sorted = new int[5][2];
return sorted;
}
public static void print_identify_hand(int identify_hand) {
if (identify_hand==1)
System.out.print("(straight flush)");
else if (identify_hand==2)
System.out.print("(four of a kind)");
else if (identify_hand==3)
System.out.print("(full house)");
else if (identify_hand==3)
System.out.print("(four of a kind)");
else if (identify_hand==4)
System.out.print("(flush)");
else if (identify_hand==5)
System.out.print("(straight)");
else if (identify_hand==6)
System.out.print("(three of a kind)");
else if (identify_hand==7)
System.out.print("(two pairs)");
else if (identify_hand==8)
System.out.print("(one pair)");
else
System.out.print("(nothing - high hand comparison)");
}
public static int compare_hands(int[][] hand1,int[][] hand2) {
// IMPLEMENT: compare 2 cards
return 1;
}
public static int identify_hand(int[][] hand) {
if (hand[0][1]==hand[1][1] && hand[1][1]==hand[2][1] && hand[2][1]==hand[3][1] && hand[3][1]==hand[4][1] && // compare that they have the same suit
hand[0][0]+1==hand[1][0] && hand[1][0]+1==hand[2][0] && hand[2][0]+1==hand[3][0] && hand[3][0]+1==hand[4][0]) // compare card numbers
return 1;
if (hand[0][0]==hand[1][0] && hand[1][0]==hand[2][0] && hand[2][0]==hand[3][0]) // compare card numbers
return 2;
if (hand[1][0]==hand[2][0] && hand[2][0]==hand[3][0] && hand[3][0]==hand[4][0]) // compare card numbers
return 2;
return 9;
}
}
Your int[][] hand objects are empty.
The generate_hand method returns an empty array, sort_hand returns another empty array, the other methods don't alter the array at all.
You end up passing empty (for int arrays, that means that each index contains 0) arrays to card_to_String, which then displays the result for c[0] == 0 and c[1] == 0, you got it...this is Ace of spades.

Java Paper Rock Scissors program

Paper rock scissors java program.
Ok so the only problem I'm having now is the player's score doesn't update until the second loop around. Any suggestions?
Thanks again Radiodef for your help!!
Updated code below.......
import java.util.Scanner;
public class RPS_Game {
public static void main(String[] args) {
char r = 'R';
char p = 'P';
char s = 'S';
char player1 = 0;
char player2 = 0;
int player1Score = 0;
int player2Score = 0;
int playCount = 0;
Scanner scan = new Scanner(System.in);
while(playCount < 3) {
System.out.print("Please enter either (R)ock, (P)aper, or (S)iccors: ");
player1 = scan.nextLine().toUpperCase().charAt(0);
System.out.print("Please enter either (R)ock, (P)aper, or (S)iccors: ");
player2 = scan.nextLine().toUpperCase().charAt(0);
int winner = winningPlayer(player1, player2);
if(winner == 0) {
System.out.print("\nIt's a tie. Nobody wins!\n");
System.out.println("\nPlayer 1: " + (player1Score += 0));
System.out.println("\nPlayer 2: " + (player2Score += 0));
}
if(winner == 1) {
System.out.print("\nPlayer 1 wins!!\n");
System.out.println("\nPlayer 1: " + player1Score++);
System.out.println("\nPlayer 2: " + (player2Score += 0));
}
if(winner == 2) {
System.out.print("\nPlayer 2 wins!!\n");
System.out.println("\nPlayer 1: " + (player1Score += 0));
System.out.println("\nPlayer 2: " + player2Score++);
}
playCount++;
}
}
public static int winningPlayer(int player1, int player2) {
//Player 1 wins
int result = 0;
if(player1 == 'R' && player2 == 'S') {
result = 1;
}
else if(player1 == 'P' && player2 == 'R') {
result = 1;
}
else if(player1 == 'S' && player2 == 'P') {
result = 1;
}
//Player 2 wins
else if(player2 == 'R' && player1 == 'S') {
result = 2;
}
else if(player2 == 'P' && player1 == 'R') {
result = 2;
}
else if(player2 == 'S' && player1 == 'P') {
result = 2;
}
return result;
}
}
Well, it seems like first you just need to move the call in to the loop:
while(playCount < 3) {
System.out.print("Please enter either (R)ock, (P)aper, or (S)iccors: ");
player1 = scan.nextLine().toUpperCase().charAt(0);
System.out.print("Please enter either (R)ock, (P)aper, or (S)iccors: ");
player2 = scan.nextLine().toUpperCase().charAt(0);
// recompute the winner each time
int winner = winningPlayer(player1, player2);
...
}
For keeping a score, you could just have a variable for each player:
int player1Score = 0;
int player2Score = 0;
while (...) {
...
if (winner == 1) {
++player1Score;
}
if (winner == 2) {
++player2Score;
}
}
Or you could do something fancier like use an array:
int[] scores = new int[3];
while (...) {
...
++scores[ winner ];
for (int i = 1; i < scores.length; ++i) {
System.out.printf("Player %d score is %d.\n", i, scores[i]);
}
}

Using a return value from a method that also has print statements

I am working on a ROCK PAPER SCISSORS project for class and am trying to use a method which prints who wins the match and also return a value I'm using to determine who has the most wins.
However in the first method when I assign the method to a value to use it it prints out the statements in the evaluate method, is there any way around this or will I have to create another method that does the same thing just with out the print statements?
(userWins and computerWins are declared and initialized in main() )
main( ) {
int userWins = 0;
int computerWins = 0;
.....
int value = winEvaluation(userChoice, computerChoice);
if (value == 1){
userWins++;
} else if (value == 2){
computerWins++;
} else{
}
System.out.prinln("user wins: " + userWins + " computer wins: " + computerWins);
}
private static int evaluate(int userChoice, int computerChoice) {
int winValue = 0;
String win = "You win.";
String lose = "I win.";
String draw = "We picked the same thing! This round is a draw.";
if( userChoice == 1) {
if(computerChoice == 1){
System.out.println(draw);
winValue = 0;
}else if(computerChoice == 2){
System.out.print("PAPER covers ROCK." );
System.out.println(lose);
winValue = 2;
}else {
System.out.print("ROCK breaks SCISSORS.");
System.out.println(win);
winValue = 1;
}
}else if (userChoice == 2 ) {
if (computerChoice == 1){
System.out.print("PAPER covers ROCK.");
System.out.println(win);
winValue = 1;
}else if (computerChoice == 2){
System.out.println(draw);
winValue = 0;
}else {
System.out.print("SCISSORS cuts PAPER.");
System.out.println(lose);
winValue = 2;
}
}else if (userChoice == 3){
if (computerChoice == 1){
System.out.print("ROCK breaks SCISSORS.");
System.out.println(lose);
winValue = 2;
}else if (computerChoice == 2){
System.out.print("SCISSORS cuts PAPER.");
System.out.println(win);
winValue = 1;
}else {
System.out.println(draw);
winValue = 0;
}
}
return winValue;
}

Categories

Resources