Tin học Rock,Paper,Scissors trong C#

duybaophanthanh@gmail.com

Học sinh mới
Thành viên
13 Tháng sáu 2021
6
12
6
[TẶNG BẠN] TRỌN BỘ Bí kíp học tốt 08 môn
Chắc suất Đại học top - Giữ chỗ ngay!!

ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.

Mã:
using System;
namespace ha
{
 class ha{
 public static void Main(String[]args)
 {
 string userChoice;
 int userPoint = 0;
 int computerPoint = 0;
 bool running = true;
 string youQuit;
 int playerTry = 0;

 Random randChoice = new Random();
 int computerChoice = randChoice.Next(1,4);

 while(running){
 Console.WriteLine("-------------------------");
 Console.WriteLine("1.Rock");
 Console.WriteLine("2.Paper");
 Console.WriteLine("3.Scissors");
 Console.Write("Player : ");
 userChoice = Console.ReadLine();

 /*

 Ghi chu:
 1 Rock
 2 Paper
 3 Scissors


 */

 switch (computerChoice){
 case 1:
 if (userChoice == "1")
 {
 Console.WriteLine("You : Rock");
 Console.WriteLine("Computer : Rock");
 Console.WriteLine("DRAW!");
 userPoint += 0;
 computerPoint += 0;
 playerTry += 1;
 }
 else if (userChoice == "2")
 {
 Console.WriteLine("You : Paper");
 Console.WriteLine("Computer : Rock");
 Console.WriteLine("You WON!");
 userPoint += 1;
 computerPoint += 0;
 playerTry += 1;
 }
 else if (userChoice == "3")
 {
 Console.WriteLine("You : Scissors");
 Console.WriteLine("Computer : Rock");
 Console.WriteLine("Computer WON!");
 userPoint += 0;
 computerPoint += 1;
 playerTry += 1; 
 }
 break;

 case 2:
 if (userChoice == "1")
 {
 Console.WriteLine("You : Rock");
 Console.WriteLine("Computer : Paper");
 Console.WriteLine("Computer WON!");
 userPoint += 0;
 computerPoint += 1;
 playerTry += 1;
 }
 else if (userChoice == "2")
 {
 Console.WriteLine("You : Paper");
 Console.WriteLine("Computer : Paper");
 Console.WriteLine("DRAW!");
 userPoint += 0;
 computerPoint += 0;
 playerTry += 1;
 }
 else if (userChoice == "3")
 {
 Console.WriteLine("You : Scissors");
 Console.WriteLine("Computer : Paper");
 Console.WriteLine("You WON!");
 userPoint += 1;
 computerPoint += 0;
 playerTry += 1; 
 }
 break;

 case 3:
 if (userChoice == "1")
 {
 Console.WriteLine("You : Rock");
 Console.WriteLine("Computer : Scissors");
 Console.WriteLine("You WON!");
 userPoint += 1;
 computerPoint += 0;
 playerTry += 1;
 }
 else if (userChoice == "2")
 {
 Console.WriteLine("You : Paper");
 Console.WriteLine("Computer : Scissors");
 Console.WriteLine("Computer WON!");
 userPoint += 0;
 computerPoint += 1;
 playerTry += 1;
 }
 else if (userChoice == "3")
 {
 Console.WriteLine("You : Scissors");
 Console.WriteLine("Computer : Scissors");
 Console.WriteLine("DRAW!");
 userPoint += 0;
 computerPoint += 0;
 playerTry += 1; 
 }
 break;

 }
 if (playerTry == 3)
 {
 Console.WriteLine("----------------------------");
 Console.WriteLine("Do you wanna continue? (N or n or no for no and type anything to continue!)");
 Console.Write("You : ");
 youQuit = Console.ReadLine();

 if (youQuit == "N" || youQuit == "no" || youQuit == "n")
 {
 break;
 }
 else
 {
 playerTry = 0;
 }
 }

 }
 Console.WriteLine("----------------------------");
 Console.WriteLine("Your Score: "+userPoint);
 Console.WriteLine("Computer Score: "+computerPoint);

 if (userPoint == computerPoint)
 {
 Console.WriteLine("----------------------------");
 Console.WriteLine("Comment from Dev: Both of you are tie!");
 }else if (userPoint > computerPoint)
 {
 Console.WriteLine("----------------------------");
 Console.WriteLine("Comment from Dev: Hey,you're pretty good!");
 }else if (userPoint < computerPoint)
 {
 Console.WriteLine("----------------------------");
 Console.WriteLine("Comment from Dev: You SOOOO BAD!");
 }

 Console.ReadLine(); 
 }
 }
}
 
  • Like
Reactions: Duong 2007
Top Bottom