C#

Yacht Dice

전라남도교육지원청 2024. 10. 7. 15:25
// made by celbeing
// tistory: celbeing.tistory.com
// baekjoon: @kimsd1983
// atcoder: @kimsd1983

using System;

class YachtDICE{
  static int[] Dice = new int[5];
  static int[] DiceCount = new int[7];
  static bool[] Hold = new bool[5]; // T=Hold
  static int RollCount = 0;
  static int TurnCount = 0;
  static bool Turn = true; // T=1, F=2
	static bool ContinueGame = true;
	static string UserInput;
	static string CurrentSystemMessage;
  static Random rnd = new Random();
	static Player p1 = new Player();
	static Player p2 = new Player();

  static void Main(){
		Console.Clear();
		HowToPlay();
		while(ContinueGame){
			Console.Clear();
			Game();
		}
  }

	static void HowToPlay(){
		Console.WriteLine("--How to Play" + ("").PadRight(41, '-'));
		Console.WriteLine();
		Console.WriteLine("  Enter your comand on console.");
		Console.WriteLine("  1  ~  6 : Sum of choosen number.");
		Console.WriteLine("     c    : Sum all numbers.");
		Console.WriteLine("     k    : 4 of a Kind");
		Console.WriteLine("     f    : Full House");
		Console.WriteLine("     s    : Small Straight");
		Console.WriteLine("     l    : Large Straight");
		Console.WriteLine("     y    : Yacht");
		Console.WriteLine("     r    : Reroll dice without held.");
		Console.WriteLine("  h1 ~ h5 : Hold die number 1 ~ 5.");
		Console.WriteLine("   score  : Print current score board.");
		Console.WriteLine("   clear  : Clear console.");
		Console.WriteLine();
		Console.WriteLine("(Refer to Nintendo 51 Worldwide Games Yacht Dice rule)");
		Console.WriteLine(("").PadRight(54, '-'));
		Console.WriteLine();
		Console.WriteLine("  Press enter to start game.");
		Console.ReadLine();
	}

  static void Game(){
    while(TurnCount < 24){
      RollDice();
      Check();
    }
		Console.Clear();
		ScoreBoard();
		Console.WriteLine();
		Console.WriteLine(p1.Score[0] > p2.Score[0] ? "   [Winner is Player 1]" : 
		(p1.Score[0] == p2.Score[0] ? "          [Draw]" : "   [Winner is Player 2]"));
		Console.WriteLine();
		Console.WriteLine(" Press enter for new game");
		Console.ReadLine();
  }

  static void RollDice(){
    for(int i = 0; i < 5; i++){
      if(!Hold[i]){
        Dice[i] = rnd.Next(1,7);
      }
    }
		RollCount++;
    PutDice();
  }
  static void PutDice(){
    CurrentSystemMessage = (Hold[0] ? "\n[" : "\n ") + Dice[0] + (Hold[0] ? "]" : " ")
		 + (Hold[1] ? "[" : " ") + Dice[1] + (Hold[1] ? "]" : " ") + (Hold[2] ? "[" : " ")
		 + Dice[2] + (Hold[2] ? "]" : " ") + (Hold[3] ? "[" : " ") + Dice[3]
		 + (Hold[3] ? "]" : " ") + (Hold[4] ? "[" : " ") + Dice[4]
		 + (Hold[4] ? "]\nP" : " \nP") +(Turn ? "1" : "2") + " Enter Order : ";
    Console.Write(CurrentSystemMessage);
  }
  static void ScoreBoard(){
		p1.Score[0] = 0;
		p2.Score[0] = 0;
    p1.Score[7] = 0;
    p2.Score[7] = 0;
    for(int i = 1; i < 7; i++){
      p1.Score[7] += p1.Score[i];
      p2.Score[7] += p2.Score[i];
    }
    for(int i = 8; i < 14; i++){
      p1.Score[0] += p1.Score[i];
      p2.Score[0] += p2.Score[i];
    }
    p1.Score[0] += p1.Score[7];
    p2.Score[0] += p2.Score[7];
    if(p1.Score[7] > 62) p1.Score[0] += 35;
    if(p2.Score[7] > 62) p2.Score[0] += 35;
		Console.WriteLine();
    Console.WriteLine("             P1.... P2....");
    Console.WriteLine($"Aces         {p1.Score[1],6} {p2.Score[1],6}");
    Console.WriteLine($"Deuces       {p1.Score[2],6} {p2.Score[2],6}");
    Console.WriteLine($"Threes       {p1.Score[3],6} {p2.Score[3],6}");
    Console.WriteLine($"Fours        {p1.Score[4],6} {p2.Score[4],6}");
    Console.WriteLine($"Fives        {p1.Score[5],6} {p2.Score[5],6}");
    Console.WriteLine($"Sixes        {p1.Score[6],6} {p2.Score[6],6}");
    Console.WriteLine(("").PadRight(26, '-'));
    Console.WriteLine($"Subtotal     {p1.Score[7],3}/63 {p2.Score[7],3}/63");
    Console.WriteLine($"Bonus           " + (p1.Score[7] > 62 ? "+35" : "  0") + (p2.Score[7] > 62 ? "    +35" : "      0"));
    Console.WriteLine(("").PadRight(26, '-'));
    Console.WriteLine($"Choice       {p1.Score[8],6} {p2.Score[8],6}");
    Console.WriteLine($"4 of a Kind  {p1.Score[9],6} {p2.Score[9],6}");
    Console.WriteLine($"Full House   {p1.Score[10],6} {p2.Score[10],6}");
    Console.WriteLine($"S.Straight   {p1.Score[11],6} {p2.Score[11],6}");
    Console.WriteLine($"L.Straight   {p1.Score[12],6} {p2.Score[12],6}");
    Console.WriteLine($"Yacht        {p1.Score[13],6} {p2.Score[13],6}");
    Console.WriteLine(("").PadRight(26, '-'));
    Console.WriteLine($"Total        {p1.Score[0],6} {p2.Score[0],6}");
  }

	static int KindOf4(){
		for(int i = 0; i < 7; i++) DiceCount[i] = 0;
    for(int i = 0; i < 5; i++) DiceCount[Dice[i]]++;
    for(int i = 1; i < 7; i++){
      if(DiceCount[i] >= 4){
				for(int j = 0; j < 5; j++) DiceCount[0] += Dice[j];
				return DiceCount[0];
			}
    }
    return 0;
	}
	static int FullHouse(){
		for(int i = 0; i < 7; i++) DiceCount[i] = 0;
    int temp = 0;
    for(int i = 0; i < 5; i++) DiceCount[Dice[i]]++;
    for(int i = 1; i < 7; i++){
      if(DiceCount[i] == 5) return i * 5;
      else if(DiceCount[i] == 2) temp += i * 2;
      else if(DiceCount[i] == 3) temp += i * 3;
      else if(DiceCount[i] == 0) continue;
      else return 0;
    }
    return temp;
	}
	static int SmallStraight(){
		for(int i = 0; i < 7; i++) DiceCount[i] = 0;
    Array.Sort(Dice);
    for(int i = 1; i < 5; i++){
      if(Dice[i - 1] + 1 == Dice[i]) DiceCount[1]++;
    }
    return DiceCount[1] >= 3 ? 15 : 0;
	}
	static int LargeStraight(){
		for(int i = 0; i < 7; i++) DiceCount[i] = 0;
    Array.Sort(Dice);
    for(int i = 1; i < 5; i++){
      if(Dice[i - 1] + 1 == Dice[i]) DiceCount[1]++;
    }
    return DiceCount[1] == 4 ? 30 : 0;
	}
	static int Yacht(){
		for(int i = 0; i < 7; i++) DiceCount[i] = 0;
    for(int i = 0; i < 5; i++) DiceCount[Dice[i]]++;
    for(int i = 1; i < 7; i++){
      if(DiceCount[i] == 5) return 50;
      else if(DiceCount[i] == 0) continue;
      else return 0;
    }
    return 0;
	}
  static void Check(){
		int DiceScore = 0;
		bool InputChecker = true;
		while(InputChecker){
			UserInput = Console.ReadLine();
			InputChecker = !InputChecker;
    	switch(UserInput){
    	  case "1":
					if(Turn ? p1.Checked[1] : p2.Checked[1]) goto default;
					for(int i = 0; i < 5; i++){
						if(Dice[i] == 1) DiceScore += 1;
					}
					if(Turn){
						p1.Checked[1] = true;
						p1.Score[1] = DiceScore;
					}
					else{
						p2.Checked[1] = true;
						p2.Score[1] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "2":
					if(Turn ? p1.Checked[2] : p2.Checked[2]) goto default;
					for(int i = 0; i < 5; i++){
						if(Dice[i] == 2) DiceScore += 2;
					}
					if(Turn){
						p1.Checked[2] = true;
						p1.Score[2] = DiceScore;
					}
					else{
						p2.Checked[2] = true;
						p2.Score[2] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "3":
					if(Turn ? p1.Checked[3] : p2.Checked[3]) goto default;
					for(int i = 0; i < 5; i++){
						if(Dice[i] == 3) DiceScore += 3;
					}
					if(Turn){
						p1.Checked[3] = true;
						p1.Score[3] = DiceScore;
					}
					else{
						p2.Checked[3] = true;
						p2.Score[3] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "4":
					if(Turn ? p1.Checked[4] : p2.Checked[4]) goto default;
					for(int i = 0; i < 5; i++){
						if(Dice[i] == 4) DiceScore += 4;
					}
					if(Turn){
						p1.Checked[4] = true;
						p1.Score[4] = DiceScore;
					}
					else{
						p2.Checked[4] = true;
						p2.Score[4] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "5":
					if(Turn ? p1.Checked[5] : p2.Checked[5]) goto default;
					for(int i = 0; i < 5; i++){
						if(Dice[i] == 5) DiceScore += 5;
					}
					if(Turn){
						p1.Checked[5] = true;
						p1.Score[5] = DiceScore;
					}
					else{
						p2.Checked[5] = true;
						p2.Score[5] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "6":
					if(Turn ? p1.Checked[6] : p2.Checked[6]) goto default;
					for(int i = 0; i < 5; i++){
						if(Dice[i] == 6) DiceScore += 6;
					}
					if(Turn){
						p1.Checked[6] = true;
						p1.Score[6] = DiceScore;
					}
					else{
						p2.Checked[6] = true;
						p2.Score[6] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "c":
					if(Turn ? p1.Checked[8] : p2.Checked[8]) goto default;
					foreach(int d in Dice) DiceScore += d;
					if(Turn){
						p1.Checked[8] = true;
						p1.Score[8] = DiceScore;
					}
					else{
						p2.Checked[8] = true;
						p2.Score[8] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "k":
					if(Turn ? p1.Checked[9] : p2.Checked[9]) goto default;
					DiceScore = KindOf4();
					if(Turn){
						p1.Checked[9] = true;
						p1.Score[9] = DiceScore;
					}
					else{
						p2.Checked[9] = true;
						p2.Score[9] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "f":
					if(Turn ? p1.Checked[10] : p2.Checked[10]) goto default;
          DiceScore = FullHouse();
					if(Turn){
						p1.Checked[10] = true;
						p1.Score[10] = DiceScore;
					}
					else{
						p2.Checked[10] = true;
						p2.Score[10] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "s":
					if(Turn ? p1.Checked[11] : p2.Checked[11]) goto default;
          DiceScore = SmallStraight();
					if(Turn){
						p1.Checked[11] = true;
						p1.Score[11] = DiceScore;
					}
					else{
						p2.Checked[11] = true;
						p2.Score[11] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "l":
					if(Turn ? p1.Checked[12] : p2.Checked[12]) goto default;
          DiceScore = LargeStraight();
					if(Turn){
						p1.Checked[12] = true;
						p1.Score[12] = DiceScore;
					}
					else{
						p2.Checked[12] = true;
						p2.Score[12] = DiceScore;
					}
					DiceScore = 0;
					break;
				case "y":
					if(Turn ? p1.Checked[13] : p2.Checked[13]) goto default;
          DiceScore = Yacht();
					if(Turn){
						p1.Checked[13] = true;
						p1.Score[13] = DiceScore;
					}
					else{
						p2.Checked[13] = true;
						p2.Score[13] = DiceScore;
					}
					DiceScore = 0;
					break;
        case "score":
          InputChecker = !InputChecker;
          ScoreBoard();
          Console.Write(CurrentSystemMessage);
          break;
        case "clear":
          InputChecker = !InputChecker;
          Console.Clear();
          Console.Write(CurrentSystemMessage);
          break;
				case "h1":
          InputChecker = !InputChecker;
					Hold[0] = !Hold[0];
					PutDice();
					break;
				case "h2":
          InputChecker = !InputChecker;
					Hold[1] = !Hold[1];
					PutDice();
					break;
				case "h3":
          InputChecker = !InputChecker;
					Hold[2] = !Hold[2];
					PutDice();
					break;
				case "h4":
          InputChecker = !InputChecker;
					Hold[3] = !Hold[3];
					PutDice();
					break;
				case "h5":
          InputChecker = !InputChecker;
					Hold[4] = !Hold[4];
					PutDice();
					break;
				case "r":
					InputChecker = !InputChecker;
					if(RollCount == 3){
						Console.WriteLine("You spent all roll.");
						Console.Write(CurrentSystemMessage);
						break;
					}
					RollDice();
					break;
      	default:
          InputChecker = !InputChecker;
          Console.WriteLine("Input not accepted. Please check your input.");
          Console.Write(CurrentSystemMessage);
          break;
    	}
		}
    for(int i = 0; i < 7; i++) DiceCount[i] = 0;
		if(!InputChecker){
			for(int i = 0; i < 5; i++) Hold[i] = false;
			Turn = !Turn;
			RollCount = 0;
			TurnCount++;
		}
  }
}

class Player{
  public int[] Score = new int[14];
	public bool[] Checked = new bool[14];
  // ttl, 1, 2, 3, 4, 5, 6, bonus, choice, 4kind, fh, ss, ls, y
}