U E D R , A S I H C RSS

데블스캠프2005/금요일/One Card/이동현

원카드 게임

컴퓨터대 컴퓨터로 하게 만들었습니다

~cpp 
import java.util.*;
import java.util.*;

class Card{
	int num;
	int face;	
}

class Cards{
	ArrayList arr = new ArrayList();
	void add(Card card){
		arr.add(card);
	}	
	Card delete(int n){
		return (Card)arr.remove(n);
	}
	int search(int num, int face){
		for(int i=0; i<arr.size(); i++){
			Card card = ((Card)arr.get(i));
			if(card.face == face)
				return i;
			if(card.num == num)
				return i;
		}
		return -1;
	}
	int size(){
		return arr.size();
	}
	Card retTop(){
		return (Card)arr.get(arr.size()-1);		
	}
	void showCards(){
		Card card;	
		for(int i=0; i<arr.size(); i++){
			card = (Card)arr.get(i);		
			System.out.print(i+".("+card.face+" "+card.num+") ");
		}
		System.out.print("\n");
	}
}

public class OneCard {
	boolean isOneCard = false;
	Cards comCards = new Cards();
	Cards playerCards= new Cards();
	Cards discard = new Cards();
	Cards stack = new Cards();
	
	void initCards(){
		//52장의 카드를 쌓아놓음
		for(int i=0; i<4; i++){
			for(int j=0; j<13; j++){
				Card card = new Card();
				card.num = j+1;
				card.face = i;
				stack.add(card);				
			}				
		}
		//그중 10장을 컴퓨터와 플레이어에게 나눠줌
		for(int i=0; i<10; i++){
			Random rand = new Random();
			comCards.add(stack.delete(rand.nextInt(stack.size()-1)));
			playerCards.add(stack.delete(rand.nextInt(stack.size()-1)));
		}
		Random rand = new Random();
		discard.add(stack.delete(rand.nextInt(comCards.size())));
	}
	void showCards(){
		System.out.println("컴퓨터카드");
		comCards.showCards();
		System.out.println("마지막으로 낸 카드");
		System.out.println(discard.retTop());
		System.out.println("플레이어카드");
		playerCards.showCards();
		System.out.println("");
	}
	
	void run(){
		while(true){
			//컴퓨터의 차례
			Random rand = new Random();
			int choice = comCards.search(discard.retTop().num,discard.retTop().face);
			if(choice == -1){			
				comCards.add(stack.delete(rand.nextInt(comCards.size())));
			}		
			else{
				discard.add(comCards.delete(choice));
			}
			if(comCards.size() == 1){
				System.out.println("컴퓨터승리");
				break;
			}
			showCards();
			//플레이어의 차례
			choice = playerCards.search(discard.retTop().num,discard.retTop().face);
			if(choice == -1){			
				playerCards.add(stack.delete(rand.nextInt(comCards.size())));
			}		
			else{
				discard.add(playerCards.delete(choice));
			}
			if(playerCards.size() == 1){
				System.out.println("플레이어승리");
				break;
			}
			showCards();
			
		}
	}
	public static void main(String[] args) {
		OneCard game = new OneCard();
		game.initCards();
		game.run();
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:59
Processing time 0.0118 sec