Wykonywanie zdalnych metod przez klienta [mini-gra]

0

Witam,

napisałem małą symulację, gry w pokera.
Mam klasy:

  • Card,
  • Deck [odpowiedzialna za tworzenie talii, tasowanie],
  • Player [przypisująca 5 kart graczowi, sprawdzanie czy karty gracza po wymianie maja pare, trojke itp]
  • Table [kilka metod odwołujących się do klasy Player czyli: tworzenie nowych graczy, wymiana kart, pokazanie kart dla gracza x, rozdawanie kart graczom]

Problem mój leży w tym, że chciałbym aby gracze mogli łączyć się za pomocą klientów do serwera.
Napisałem prostą aplikację na bazie Socket, lecz jedynie co potrafię w niej zrobić to w sumie wyświetlenie jakiegoś tekstu na ekranie.

Tu moje pytanie. Co należy zrobić aby klient mógł wykonywać metody zawarte w Table? Po połączeniu miałaby się wykonać metoda ShowCardsForPlayer(1) np. potem pytanie do klienta czy chce wymienić jakieś karty (to potrafię zrobić) jeśli tak to wykonanie metody changeCardsForPlayer(1) i na koncu wyswietlenie jakie ma karty i wyniku.

Prosiłbym tutaj o pomoc w jakie biblioteki należy się zagłębić, aby takie rozwiązanie wykonać.
Czy trzeba będzie stworzyć interface?

Pozdrawiam
formalny

0

Moze spróbujesz tu użyć Java RMI? Bo Socketami to się ukodzisz na śmierć.

0

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


	}

}
0
Shalom napisał(a):

Moze spróbujesz tu użyć Java RMI? Bo Socketami to się ukodzisz na śmierć.

Hmm.. rozumiem. Spróbuję coś znaleźć na ten temat.

1 użytkowników online, w tym zalogowanych: 0, gości: 1