gpt4 book ai didi

java - 建筑纸牌游戏需要相同的纸牌才能匹配值(value)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:47:03 26 4
gpt4 key购买 nike

我正在为一项任务构建一个 war 卡牌游戏我已经构建了游戏并且它正在运行但我遇到了诸如卡片 10 黑桃 jack 的值(value)低于卡片 23 红心 jack 的值(value)。寻找有关能够比较卡片以查看它们是否相等的最佳方法的建议。

下面是我目前的代码:

import java.util.Random;
import java.util.Scanner;

public class WarGame {

public static void main(String a[]) {
DeckOfCards d = new DeckOfCards();
int input = 0;
int computer = 0;
int you = 0;
Scanner sc = new Scanner(System.in);
System.out.print("\n1.To play\n2.Exit\nEnter the choice:");
input = sc.nextInt();
while (input != 2) {
if (input == 1) {
System.out.print("\n\nEnter the value of the card:");
int card = sc.nextInt();
if (card >= 0 && card <= 51) {
int systemCard = d.computerTurn(card);
System.out.println("Your card - " + d.inputCard(card));
System.out.println("Computer card - " + d.inputCard(systemCard));
if(systemCard > card)
++computer;
else
++you;
System.out.println("The winner is " + (systemCard > card ? "Computer" : "You"));
} else {
System.out.println("Invalid card");
}
}
else {
System.out.println("That is an invalid selection please choose 1 or 2.");
}
System.out.print("\n1.To play\n2.Exit\nEnter the choice:");
input = sc.nextInt();
}
System.out.println("Total Wins by Computer: "+ computer);
System.out.println("Total Wins by You: "+ you);
if (computer > you)
System.out.println("Computer is the champion");
else if (computer == you)
System.out.println("Its a Tie");
else
System.out.println("You are the champion");
}
}

class DeckOfCards {
String suits[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
Random ran = new Random();
int systemWin = 0;
int playerWin = 0;
String inputCard(int card) {
int suit = card / 13; //assigning suit to your card
int rank = card % 13; //Assigning rank to your card
String out = "";
switch (rank) { //Setting up face cards for the cases
case 0:
out = "Ace of " + suits[suit];
break;
case 10:
out = "Jack of " + suits[suit];
break;
case 11:
out = "Queen of " + suits[suit];
break;
case 12:
out = "King of " + suits[suit];
break;
default:
out = rank + 1 + " of " + suits[suit]; //Adding one to remainder so it will go from 2-10 instead of 1-9
break;
}
return out;
}

int computerTurn(int playerRank) { //Keeping track of the wins for computer and player
int systemRank = this.ran.nextInt(51);
if (systemRank > playerRank)
systemWin++;
else
playerWin++;
return systemRank;
}
}

最佳答案

我认为您是在将索引与您的套牌进行比较,而不是卡片值(value)本身。如果我理解,您想将 d.inputCard(card) 与 d.inputCard(systemCard) 进行比较,而不是将 card 与 systemCard 进行比较。但当然,这是一个字符串。很难遵循代码:-)。

关于java - 建筑纸牌游戏需要相同的纸牌才能匹配值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51433643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com