Pages

Kamis, 27 Maret 2014

How to make A Simple game with Console Application On C# (Suit China)


This is example How to make A Simple game with Console Application On C#?
Nah ini contoh kodingan  Sederhana untuk membuat game suit china . tau kan suit china ? yang isinya gunting,kertas dan batu . well, langsung aja di check Syntaxnya .  Cekidot !


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;
using System.IO;

namespace PROJECT
{
    class Program
    {

        public static void PlayervsComputer()
        {
            Console.Clear();
            string playerChoice;
            string computerChoice;
            string[] RPS = { "B", "K", "G" };
            Random ranNumberGenerator = new Random();


            int LivesCount = 3;
            int Menang = 0;
            Console.WriteLine("Masukkan Nama Anda : ");
            string Nama = Console.ReadLine();
            do
            {

                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Kesempatan : {0}", LivesCount);
                Console.SetCursorPosition(62, 0);
                Console.WriteLine("Kemenangan : {0}", Menang);
                Console.ResetColor();
                Console.SetCursorPosition(0, 3);
                for (int i = 0; i < 80; i++)
                {
                    Console.Write("=");
                }
                Console.WriteLine("");
                Console.WriteLine();

                Console.Write("Pilih (B) Untuk Batu , (K) untuk Kertas, (G) Untuk Gunting : ");
                playerChoice = Console.ReadLine().ToUpper();

                switch (playerChoice)
                {
                    case "B":
                    case "K":
                    case "G":
                        computerChoice = RPS[ranNumberGenerator.Next(RPS.Length)];
                        for (int i = 0; i < 5; i++)
                        {
                            Console.Write(".");
                            Thread.Sleep(200);
                        }
                        Console.WriteLine();
                        Console.WriteLine("Pilihan Computer: " + computerChoice);
                        if (computerChoice == playerChoice)
                            {
                                Console.WriteLine("Pilihan anda dan Computer sama");
                                Console.WriteLine("Seri !");

                            }
                            else
                            {
                                switch (computerChoice)
                                {
                                    case "B":
                                        if (playerChoice == "K")
                                        {
                                            Console.WriteLine("Paper wraps Rock!");
                                            Console.WriteLine("Selamat {0} !!", Nama + " Anda Menang");
                                            Menang++;

                                        }
                                        else
                                        {
                                            Console.WriteLine("Rock smashes Scissors!");
                                            Console.WriteLine("Maaf, anda kalah !");
                                            Console.Beep();
                                            LivesCount--;
                                        }
                                        break;

                                    case "K":
                                        if (playerChoice == "B")
                                        {
                                            Console.WriteLine("Paper wraps Rock!");
                                            Console.WriteLine("Maaf, Anda Kalah!");
                                            Console.Beep();
                                            LivesCount--;
                                        }
                                        else
                                        {
                                            Console.WriteLine("Scissors cut Paper!");
                                            Console.WriteLine("Selamat {0} !!", Nama + " Anda Menang");
                                            Menang++;
                                        }
                                        break;

                                    case "G":
                                        if (playerChoice == "K")
                                        {
                                            Console.WriteLine("Scissors cut Paper!");
                                            Console.WriteLine("Maaf, Anda Kalah!");
                                            Console.Beep();
                                            LivesCount--;
                                        }
                                        else
                                        {
                                            Console.WriteLine("Rock smashes Scissors!");
                                            Console.WriteLine(" Selamat {0} !!", Nama + " Anda Menang");
                                            Menang++;
                                        }
                                        break;
                                }
                            }
                        break;

                    default:
                        Console.WriteLine("Pilihan Anda Tidak Terdapat dalam Menu !!");
                        break;
                }
                Console.ReadLine();
            } while (LivesCount != 0);
            Console.SetCursorPosition(32, 0);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Game Over !");
            Console.ResetColor();
            Console.ReadLine();
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Yellow;
            for(int i = 0 ; i < 80 ; i++)
            {
            Console.Write("=");
            Thread.Sleep(10);
            }
            Console.ResetColor();
            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine("Hello {0} !!", Nama);
            Console.WriteLine("Anda sudah bermain dengan sangat baik !");
            Console.WriteLine("Score anda adalah : {0}", Menang);
            Console.ResetColor();
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Yellow;
            for (int i = 0; i < 80; i++)
            {
                Console.Write("=");
                Thread.Sleep(10);
            }
            Console.ReadLine();
        }
       
            
        public static void PlayervsPlayer()
        {
           
            Console.WriteLine("Masukkan Nama untuk pemain Pertama : ");
            string Name1 = Console.ReadLine();
            Console.WriteLine("Masukkan Nama untuk pemain Kedua : ");
            string Name2 = Console.ReadLine();
            Console.Clear();     
           
             int LivesCount1 = 3;
            int LivesCount2 = 3;
            int Player1Menang = 0;
            int Player2Menang = 0;
           
            do
            {

                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.SetCursorPosition(0, 0);

                Console.WriteLine("Kesempatan Player 1 : {0}", LivesCount1);
                Console.SetCursorPosition(0, 1);
                Console.WriteLine("Kemenangan : {0}", Player1Menang);
                Console.WriteLine();
                Console.SetCursorPosition(50, 0);
                Console.WriteLine("Kesempatan Player 2 : {0}", LivesCount2);
                Console.SetCursorPosition(50, 1);
                Console.WriteLine("Kemengan Player 2 : {0}", Player2Menang);
                Console.WriteLine();
                Console.WriteLine("{0}", Name1 + " Mulai");
                Console.Write("Pilih B, K, G : ");
                string player1Choice = Console.ReadLine().ToUpper();
                Console.Clear();
                Console.SetCursorPosition(0, 0);
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Kesempatan Player 1 : {0}", LivesCount1);
                Console.SetCursorPosition(0, 1);
                Console.WriteLine("Kemenangan : {0}", Player1Menang);
                Console.WriteLine();
                Console.SetCursorPosition(50, 0);
                Console.WriteLine("Kesempatan Player 2 : {0}", LivesCount2);
                Console.SetCursorPosition(50, 1);
                Console.WriteLine("Kemengan Player 2 : {0}", Player2Menang);
                Console.WriteLine();
                               
                Console.WriteLine("{0}", Name2 + " Mulai");
                Console.Write("Pilih B, K, G : ");
                string Player2Choice = Console.ReadLine().ToUpper();

                switch (player1Choice)
                {
                    case "B":
                    case "K":
                    case "G":
                        Console.WriteLine("Pilihan Player 1 : " + player1Choice);
                        Console.WriteLine("Pilihan Player 2 : " + Player2Choice);

                        if (player1Choice == Player2Choice)
                        {
                            Console.WriteLine("Pilihan {0}",Name1 + "dan {1}",Name2 + " sama");
                            Console.WriteLine("Seri !");

                        }
                        else
                        {
                            switch (Player2Choice)
                            {
                                case "B":
                                    if (player1Choice == "K")
                                    {
                                        Console.WriteLine("Paper wraps Rock!");
                                        Console.WriteLine("Selamat {0} !!",Name1 + "Anda Menang");
                                        Player1Menang++;
                                        LivesCount2--;
                                    }
                                    else
                                    {
                                        Console.WriteLine("Rock smashes Scissors!");
                                        Console.WriteLine("Selamat {0} ", Name2 + " anda Menang");
                                        Player2Menang++;
                                        LivesCount1--;
                                    }
                                    break;

                                case "K":
                                    if (player1Choice == "B")
                                    {
                                        Console.WriteLine("Paper wraps Rock!");
                                        Console.WriteLine("Selamat {0} ", Name2 + " anda Menang");
                                        Player2Menang++;
                                        LivesCount1--;
                                    }
                                    else
                                    {
                                        Console.WriteLine("Scissors cut Paper!");
                                        Console.WriteLine("Selamat {0} !!", Name1 + "Anda Menang");
                                        Player1Menang++;
                                        LivesCount2--;
                                    }
                                    break;

                                case "G":
                                    if (player1Choice == "K")
                                    {
                                        Console.WriteLine("Scissors cut Paper!");
                                        Console.WriteLine("Selamat {0} ", Name2 + " anda Menang");
                                        Player2Menang++;
                                        LivesCount1--;
                                    }
                                    else
                                    {
                                        Console.WriteLine("Rock smashes Scissors!");
                                        Console.WriteLine("Selamat {0} !!", Name1 + "Anda Menang");
                                        Player1Menang++;
                                            LivesCount2--;
                                    }
                                    break;
                            }
                        }
                        break;

                    default:
                        Console.WriteLine("Pilihan Anda Tidak Terdapat dalam Menu !!");
                        break;
                }
                Console.ReadLine();
            } while((LivesCount1 !=0)||(LivesCount2 !=0));
        }
   
        static void PeraturanPermainan()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("<=================================================>");
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.BackgroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("         0 0 0     0   0  0      00000             ");
            Console.WriteLine("         0    0    0   0  0      0                 ");
            Console.WriteLine("         0 0 0     0   0  0      0000              ");
            Console.WriteLine("         0    0    0   0  0      0                 ");
            Console.WriteLine("         0     0   00000  00000  00000             ");
            Console.ResetColor();
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("<=================================================>");
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.BackgroundColor = ConsoleColor.White;
            Console.WriteLine("Game ini sangat simple dan mudah untuk dimengerti");
            Console.WriteLine("Anda Cukup Memilih Gunting (G), Kertas (K) Atau Batu (B)");
            Console.WriteLine("Disini Terdapat Dua Pilihan");
            Console.WriteLine("Jika Anda meimilih Player vs Computer, Maka Anda Akan Menghadapi Computer");
            Console.WriteLine("Kesempatan Anda Untuk Menang hanya 3 kali");
            Console.WriteLine("Gunakan feeling untuk menang agar anda mampu menjadi pemilik Nilai Tertinggi!!");
            Console.WriteLine("Jika Anda Memilih Player vs Player, Maka Anda melakukan Duel Melawan Teman Anda");
            Console.WriteLine("Anda Juga Bisa Live Chat lho dengan teman Anda");
            Console.WriteLine("Yang lebih dulu menang 3 kali kesempatan Maka dialah Pemenangnya");
            Console.WriteLine("Keep Calm and having fun with this game !!!!");
            Console.ResetColor();
            Console.ResetColor();
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.WriteLine("\nPress Any Key to Continue");
            Console.ResetColor();
            Console.ResetColor();
            Console.SetCursorPosition(32, 20);
            Console.WriteLine("--Terima Kasih--");

            Console.ReadKey();

        }

        static void TentangGame()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("Flashback ke massa kecil, game ini cukup populer dengan nama suit china");
            Console.WriteLine("game ini terdiri dari gunting, kertas dan batu");
            Console.WriteLine("andaikan anda kalah maka akan ada hukuman yang terjadi");
            Console.WriteLine("nah, game ini hadir dengan konteks yang berbeda");
            Console.WriteLine("dimana Anda bisa memecahkan rekor untuk menang");
            Console.WriteLine("Game Ini Terlahir Karena adanya Tugas Project di @CCIT-FTUI ");
            Console.WriteLine("Credits : - Anjar Puspitasari");
            Console.WriteLine("          - Falah Achmad Bagusti");
            Console.WriteLine("          - Muhammad Fahmi Rukmana");
            Console.WriteLine("Ingin Tahu bagaimana Cara membuat game ini ?");
            Console.WriteLine("Kunjungi, www.Falahachmadbagusti.blogspot.com");
            for (int i = 0; i < 43; i++)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("-");
                Thread.Sleep(20);
                Console.ResetColor();
            }
            Console.WriteLine("\n\t\tThank you !\n");
            for (int i = 0; i < 43; i++)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("-");
                Thread.Sleep(20);
                Console.ResetColor();
            }
            Console.ReadKey();

        }

       

        public static void Main()
        {
            for (int i = 0; i < 80; i++)
            {
                Console.Write("=");
                Thread.Sleep(10);
            }
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.WriteLine("Selamat datang di <>  ");
            Console.WriteLine("--> GAME TEMPOE DOLOE <--");
            Console.WriteLine("");
            Console.ResetColor();
            for (int i = 0; i < 80; i++)
            {
                Console.Write("=");
                Thread.Sleep(10);
            }
                Console.WriteLine("1. Player VS Computer");
            Console.WriteLine("2. Player VS Player");
            Console.WriteLine("3. Peraturan Permainan ");
            Console.WriteLine("4. Nilai Tertinggi");
            Console.WriteLine("5. Tentang Game");
            Console.WriteLine("6. Keluar");

            Console.Write("Pilihan Anda: ");
            int Choice = Convert.ToInt32(Console.ReadLine());
            switch (Choice)
            {
                case 1:
                    PlayervsComputer();
                    Console.Clear();
                    Main();
                    break;
                case 2:
                    PlayervsPlayer();
                    Console.Clear();
                    Main();
                    break;
                case 3 :
                    PeraturanPermainan();
                    Console.Clear();
                    Main();
                    break;
                case 5 :
                    TentangGame();
                    Console.Clear();
                    Main();
                    break;
                case 6:
                    Console.WriteLine("Terima Kasih telah mencoba game ini. jangan bosan-bosan ya !!");
                    break;
                default:
                    Console.WriteLine("Maaf, Pilihan anda tidak terdapat pada menu");
                    Console.ReadKey();
                    Main();
                    break;
            }
        }
    }
}

2 komentar:

  1. Sekarang sudah tersedia game Suit (Rock Paper Scissors/Batu Gunting Kertas) di Android. Bisa bermain online dengan teman. Download gratis di Play Store: https://play.google.com/store/apps/details?id=com.plugie.suit

    BalasHapus