Tu moje kody, żeby mieć lepszy pogląd na sprawę. W klasę Init wrzuciłem wywoływanie metod, żeby mieć lepsze pojęcie jak to powinno wyglądać po stronie klienta:
Card.java:
public class Card {
private String face;
private String suit;
//2 argumentowy konstrukor inicjalizujacy karte
public Card(String face, String suit){
super();
this.face = face;
this.suit = suit;
}
public String getFace() {
return face;
}
public void setFace(String face){
this.face = face;
}
public String getSuit(){
return suit;
}
public void setSuit(String suit){
this.suit = suit;
}
public String toString(){
return face+" of "+suit;
}
}
Deck.java:
import java.util.Random;
public class Deck {
private final String faces[] = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jake", "Queen", "King" };
private final String suits[] = { "Heart", "Diamond", "Club", "Spade" };
private Card deck[];
private final int TOTAL_CARDS = 52;
private Random randNum;
public Deck() {
deck = new Card[TOTAL_CARDS];
randNum = new Random();
for (int i = 0; i < deck.length; i++) {
deck[i] = new Card(faces[i % 13], suits[i / 13]);
}
}
public void shuffle() {
for (int i = 0; i < deck.length; i++) {
int j = randNum.nextInt(TOTAL_CARDS);
Card c = deck[i];
deck[i] = deck[j];
deck[j] = c;
}
}
public Card getCard(int index) {
return deck[index];
}
}
Player.java:
public class Player {
private final static int MAX_CARD = 5;
private Card cards[];
public Player(){
cards = new Card[MAX_CARD];
}
public Card[] getCards(){
return cards;
}
public Card getCardAtIndex(int index){
if(index>=0 && index<MAX_CARD)
return cards[index];
else
return null;
}
public void setCardAtIndex(Card c, int index){
if(index>=0 && index<MAX_CARD)
cards[index] = c;
}
public int countPair() {
int count = 0;
for (int i = 0; i < cards.length; i++) {
for (int j = i + 1; j < cards.length; j++) {
if (cards[i].getFace().equals(cards[j].getFace())){
count++;
}
}
}
return count;
}
}
Table.java:
public class Table {
private Player[] players;
private Deck deck;
// konstuktor gry i talii
public Table() {
// players[0] = new Player();
// players[1] = new Player();
// deck.shuffle();
}
public void Table2() {
deck = new Deck();
players = new Player[2];
System.out.println("Witaj na stole! \n");
}
public void TablePlayer(int numer) {
players[numer] = new Player();
}
public void goshuffle() {
deck.shuffle();
}
int howmanycards = 0;
// rozdaj karty graczom
public void dealCards() {
int count = 0;
for (int i = 0; i < players[0].getCards().length; i++) {
for (int j = 0; j < howmanycards; j++) {
players[j].setCardAtIndex(deck.getCard(count++), i);
howmanycards++;
}
}
}
public void Change(int gracz, int karta) {
players[gracz]
.setCardAtIndex(deck.getCard(howmanycards++), (karta - 1));
}
public void showCardsFor(int gracz) {
System.out.println("Karty gracza " + (gracz + 1) + ": \n");
for (int i = 0; i < players[gracz].getCards().length; i++) {
System.out.print("Karta nr " + (i + 1) + ": {"
+ players[gracz].getCardAtIndex(i).toString() + "} \n");
}
}
public void showCards() {
for (int i = 0; i < players.length; i++) {
System.out.print("Player " + (i + 1) + ": \n");
for (int j = 0; j < players[0].getCards().length; j++) {
System.out.print("Karta nr " + (j + 1) + ": {"
+ players[i].getCardAtIndex(j).toString() + "} \n");
}
System.out.println("\n------------------------------------");
}
}
public void howManyPairs() {
for (int i = 0; i < players.length; i++) {
System.out.print("Player " + (i + 1) + ": \n");
if (players[i].countPair() > 0)
System.out.print("\nLiczba znalezionych par:"
+ players[i].countPair() + "! ");
System.out.println("\n------------------------------------");
}
}
}
Init.java:
import java.util.Scanner;
public class Init {
public static void main(String[] args) {
Table table = new Table();
table.Table2(); //tworzenie gry dla 2 graczy
table.TablePlayer(0); //metoda mialaby sie wykonac po polaczeniu klienta nr 1
table.TablePlayer(1); //metoda mialaby sie wykonac po polaczeniu klienta nr 2
table.goshuffle();
table.dealCards();
table.showCardsFor(0); //ta metoda mialaby sie wyswietlic u klienta, mam z tym problem
System.out.println("Ile kart chcesz wymienic? ");
int howmany;
Scanner odczyt = new Scanner(System.in);
howmany = odczyt.nextInt();
int numberofcard;
System.out.println("Podaj numery ");
for (int i = 0; i < howmany; i++) {
numberofcard = odczyt.nextInt();
table.Change(0, numberofcard);
}
table.showCardsFor(1);
System.out.println("Ile kart chcesz wymienic? ");
howmany = odczyt.nextInt();
System.out.println("Podaj numery ");
for (int i = 0; i < howmany; i++) {
numberofcard = odczyt.nextInt();
table.Change(1, numberofcard);
}
table.showCardsFor(0); //wyswietlenie u klienta nr 1 po wymianie
table.showCardsFor(1); //wyswietlenie u klienta nr 2 po wymianie
table.howManyPairs(); //wyniki u graczy, na razie dla 1 pary zeby nie mieszac w kodzie
}
}