- 13 minutes to read
가위 바위 보 게임
이번에는 간단한 가위 바위 보 게임 프로그램을 만들어보겠습니다.
코드: RockPaperScissors.cs
using System;
class RockPaperScissors
{
static void Main()
{
int iRandom = 0; // 1(가위), 2(바위), 3(보)
int iSelection = 0; // 사용자 입력(1~3)
string[] choice = { "가위", "바위", "보" };
// 컴퓨터의 랜덤값 지정
iRandom = (new Random()).Next(1, 4);
Console.Write("1(가위), 2(바위), 3(보) 입력 : _\b");
iSelection = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n 사용자 : {0}", choice[iSelection - 1]);
Console.WriteLine(" 컴퓨터 : {0}\n", choice[iRandom - 1]);
// 결과 출력
if (iSelection == iRandom)
{
Console.WriteLine("비김");
}
else
{
switch (iSelection)
{
case 1: Console.WriteLine((iRandom == 3) ? "승" : "패"); break;
case 2: Console.WriteLine((iRandom == 1) ? "승" : "패"); break;
case 3: Console.WriteLine((iRandom == 2) ? "승" : "패"); break;
}
}
}
}
1(가위), 2(바위), 3(보) 입력 : 2
사용자 : 바위
컴퓨터 : 바위
비김
사용자로부터 1, 2, 3을 입력받아 이에 해당하는 랜덤 값과 비교를 해서 간단한 가위, 바위, 보 프로그램을 작성할 수 있습니다. 참고로, 위 프로그램에서는 1, 2, 3 이외의 값이 입력되면 에러가 발생하니 예외 처리 및 반복 등의 코드는 독자 스스로 업그레이드해보길 바랍니다.
가위 바위 보 게임
이번에는 간단한 가위 바위 보 게임 프로그램을 만들어보겠습니다.
코드: RockPaperScissors.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int iRandom = 0; // 1(가위), 2(바위), 3(보)
int iSelection = 0; // 사용자 입력(1~3)
const char *choice[] = { "가위", "바위", "보" };
// 컴퓨터의 랜덤값 지정
srand(time(NULL));
iRandom = (rand() % 3) + 1;
printf("1(가위), 2(바위), 3(보) 입력: ");
scanf("%d", &iSelection);
printf("\n 사용자: %s\n", choice[iSelection - 1]);
printf(" 컴퓨터: %s\n\n", choice[iRandom - 1]);
// 결과 출력
if (iSelection == iRandom)
{
printf("비김");
}
else
{
switch (iSelection)
{
case 1: printf((iRandom == 3) ? "승" : "패"); break;
case 2: printf((iRandom == 1) ? "승" : "패"); break;
case 3: printf((iRandom == 2) ? "승" : "패"); break;
}
}
return 0;
}
1(가위), 2(바위), 3(보) 입력: 2
사용자: 바위
컴퓨터: 바위
비김
사용자로부터 1, 2, 3을 입력받아 이에 해당하는 랜덤 값과 비교를 해서 간단한 가위, 바위, 보 프로그램을 작성할 수 있습니다. 참고로, 위 프로그램에서는 1, 2, 3 이외의 값이 입력되면 에러가 발생하니 예외 처리 및 반복 등의 코드는 독자 스스로 업그레이드해보길 바랍니다.
가위 바위 보 게임
이번에는 간단한 가위 바위 보 게임 프로그램을 만들어보겠습니다.
코드: RockPaperScissors.java
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
int iRandom = 0; // 1(가위), 2(바위), 3(보)
int iSelection = 0; // 사용자 입력(1~3)
String[] choice = { "가위", "바위", "보" };
// 컴퓨터의 랜덤값 지정
Random rand = new Random();
iRandom = rand.nextInt(3) + 1;
Scanner scanner = new Scanner(System.in);
System.out.print("1(가위), 2(바위), 3(보) 입력: ");
iSelection = scanner.nextInt();
System.out.println("\n 사용자: " + choice[iSelection - 1]);
System.out.println(" 컴퓨터: " + choice[iRandom - 1] + "\n");
// 결과 출력
if (iSelection == iRandom) {
System.out.println("비김");
} else {
switch (iSelection) {
case 1: System.out.println((iRandom == 3) ? "승" : "패"); break;
case 2: System.out.println((iRandom == 1) ? "승" : "패"); break;
case 3: System.out.println((iRandom == 2) ? "승" : "패"); break;
}
}
scanner.close();
}
}
1(가위), 2(바위), 3(보) 입력: 2
사용자: 바위
컴퓨터: 바위
비김
사용자로부터 1, 2, 3을 입력받아 이에 해당하는 랜덤 값과 비교를 해서 간단한 가위, 바위, 보 프로그램을 작성할 수 있습니다. 참고로, 위 프로그램에서는 1, 2, 3 이외의 값이 입력되면 에러가 발생하니 예외 처리 및 반복 등의 코드는 독자 스스로 업그레이드해보길 바랍니다.
가위 바위 보 게임
이번에는 간단한 가위 바위 보 게임 프로그램을 만들어보겠습니다.
코드: RockPaperScissors.py
import random
# 1(가위), 2(바위), 3(보)
i_random = 0
i_selection = 0
choice = ["가위", "바위", "보"]
# 컴퓨터의 랜덤값 지정
i_random = random.randint(1, 3)
i_selection = int(input("1(가위), 2(바위), 3(보) 입력: "))
print("\n 사용자: ", choice[i_selection - 1])
print(" 컴퓨터: ", choice[i_random - 1], "\n")
# 결과 출력
if i_selection == i_random:
print("비김")
else:
if i_selection == 1:
print("승" if i_random == 3 else "패")
elif i_selection == 2:
print("승" if i_random == 1 else "패")
elif i_selection == 3:
print("승" if i_random == 2 else "패")
1(가위), 2(바위), 3(보) 입력: 2
사용자: 바위
컴퓨터: 바위
비김
사용자로부터 1, 2, 3을 입력받아 이에 해당하는 랜덤 값과 비교를 해서 간단한 가위, 바위, 보 프로그램을 작성할 수 있습니다. 참고로, 위 프로그램에서는 1, 2, 3 이외의 값이 입력되면 에러가 발생하니 예외 처리 및 반복 등의 코드는 독자 스스로 업그레이드해보길 바랍니다.
가위 바위 보 게임
이번에는 간단한 가위 바위 보 게임 프로그램을 만들어보겠습니다.
코드: RockPaperScissors.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>가위 바위 보 게임</title>
<script>
function playGame() {
const choice = ["가위", "바위", "보"];
const i_random = Math.floor(Math.random() * 3) + 1;
const i_selection = parseInt(document.getElementById("input").value);
document.getElementById("userChoice").innerText = choice[i_selection - 1];
document.getElementById("computerChoice").innerText = choice[i_random - 1];
if (i_selection === i_random) {
document.getElementById("result").innerText = "비김";
} else {
if (i_selection === 1) {
document.getElementById("result").innerText = (i_random === 3) ? "승" : "패";
} else if (i_selection === 2) {
document.getElementById("result").innerText = (i_random === 1) ? "승" : "패";
} else if (i_selection === 3) {
document.getElementById("result").innerText = (i_random === 2) ? "승" : "패";
}
}
}
</script>
</head>
<body>
<h1>가위 바위 보 게임</h1>
<p>1(가위), 2(바위), 3(보) 입력: <input type="number" id="input" min="1" max="3"></p>
<button onclick="playGame()">게임 시작</button>
<p>사용자: <span id="userChoice"></span></p>
<p>컴퓨터: <span id="computerChoice"></span></p>
<p>결과: <span id="result"></span></p>
</body>
</html>
위 HTML 파일을 웹 브라우저에서 열고, 1, 2, 3 중 하나를 입력한 후 게임 시작 버튼을 누르면 가위, 바위, 보 게임을 진행할 수 있습니다. 참고로, 위 프로그램에서는 1, 2, 3 이외의 값이 입력되면 에러가 발생하니 예외 처리 및 반복 등의 코드는 독자 스스로 업그레이드해보길 바랍니다.
가위 바위 보 게임
이번에는 간단한 가위 바위 보 게임 프로그램을 만들어보겠습니다.
코드: RockPaperScissors.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
int main()
{
int i_random = 0; // 1(가위), 2(바위), 3(보)
int i_selection = 0; // 사용자 입력(1~3)
std::string choice[] = { "가위", "바위", "보" };
// 컴퓨터의 랜덤값 지정
srand(time(0));
i_random = rand() % 3 + 1;
std::cout << "1(가위), 2(바위), 3(보) 입력 : ";
std::cin >> i_selection;
std::cout << "\n 사용자 : " << choice[i_selection - 1];
std::cout << "\n 컴퓨터 : " << choice[i_random - 1] << std::endl;
// 결과 출력
if (i_selection == i_random)
{
std::cout << "비김";
}
else
{
switch (i_selection)
{
case 1: std::cout << ((i_random == 3) ? "승" : "패"); break;
case 2: std::cout << ((i_random == 1) ? "승" : "패"); break;
case 3: std::cout << ((i_random == 2) ? "승" : "패"); break;
}
}
return 0;
}
1(가위), 2(바위), 3(보) 입력 : 2
사용자 : 바위
컴퓨터 : 바위
비김
가위 바위 보 게임
이번에는 간단한 가위 바위 보 게임 프로그램을 만들어보겠습니다.
코드: RockPaperScissors.go
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
var i_random int // 1(가위), 2(바위), 3(보)
var i_selection int // 사용자 입력(1~3)
choice := []string{"가위", "바위", "보"}
// 컴퓨터의 랜덤값 지정
rand.Seed(time.Now().UnixNano())
i_random = rand.Intn(3) + 1
fmt.Print("1(가위), 2(바위), 3(보) 입력 : ")
fmt.Scan(&i_selection)
fmt.Printf("\n 사용자 : %s", choice[i_selection-1])
fmt.Printf("\n 컴퓨터 : %s\n", choice[i_random-1])
// 결과 출력
if i_selection == i_random {
fmt.Println("비김")
} else {
switch i_selection {
case 1:
if i_random == 3 {
fmt.Println("승")
} else {
fmt.Println("패")
}
case 2:
if i_random == 1 {
fmt.Println("승")
} else {
fmt.Println("패")
}
case 3:
if i_random == 2 {
fmt.Println("승")
} else {
fmt.Println("패")
}
}
}
}
1(가위), 2(바위), 3(보) 입력 : 2
사용자 : 바위
컴퓨터 : 바위
비김
가위 바위 보 게임
이번에는 간단한 가위 바위 보 게임 프로그램을 만들어보겠습니다.
코드: RockPaperScissors.rs
use rand::Rng;
use std::io;
fn main() {
let choices = ["가위", "바위", "보"];
let i_random = rand::thread_rng().gen_range(1..=3);
println!("1(가위), 2(바위), 3(보) 입력 : ");
let mut i_selection = String::new();
io::stdin().read_line(&mut i_selection).expect("입력 에러");
let i_selection: i32 = i_selection.trim().parse().expect("정수를 입력해주세요.");
println!("\n 사용자 : {}", choices[(i_selection - 1) as usize]);
println!(" 컴퓨터 : {}\n", choices[(i_random - 1) as usize]);
if i_selection == i_random {
println!("비김");
} else {
match i_selection {
1 => {
if i_random == 3 {
println!("승");
} else {
println!("패");
}
}
2 => {
if i_random == 1 {
println!("승");
} else {
println!("패");
}
}
3 => {
if i_random == 2 {
println!("승");
} else {
println!("패");
}
}
_ => println!("1, 2, 3 중 하나를 입력해주세요."),
}
}
}
1(가위), 2(바위), 3(보) 입력 : 2
사용자 : 바위
컴퓨터 : 바위
비김
가위 바위 보 게임
이번에는 간단한 가위 바위 보 게임 프로그램을 만들어보겠습니다.
코드: RockPaperScissors.ts
import * as readline from 'readline-sync';
function getRandomInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const choices = ['가위', '바위', '보'];
const iRandom = getRandomInt(1, 3);
console.log('1(가위), 2(바위), 3(보) 입력 : ');
const iSelection = parseInt(readline.question());
console.log(`\n 사용자 : ${choices[iSelection - 1]}`);
console.log(` 컴퓨터 : ${choices[iRandom - 1]}\n`);
if (iSelection === iRandom) {
console.log('비김');
} else {
switch (iSelection) {
case 1:
console.log(iRandom === 3 ? '승' : '패');
break;
case 2:
console.log(iRandom === 1 ? '승' : '패');
break;
case 3:
console.log(iRandom === 2 ? '승' : '패');
break;
default:
console.log('1, 2, 3 중 하나를 입력해주세요.');
}
}
1(가위), 2(바위), 3(보) 입력 : 2
사용자 : 바위
컴퓨터 : 바위
비김
가위 바위 보 게임
이번에는 간단한 가위 바위 보 게임 프로그램을 만들어보겠습니다.
코드: RockPaperScissors.kt
import kotlin.random.Random
fun main() {
val choices = arrayOf("가위", "바위", "보")
val iRandom = Random.nextInt(1, 4)
println("1(가위), 2(바위), 3(보) 입력 : ")
val iSelection = readLine()?.toInt() ?: 0
println("\n 사용자 : ${choices[iSelection - 1]}")
println(" 컴퓨터 : ${choices[iRandom - 1]}\n")
if (iSelection == iRandom) {
println("비김")
} else {
when (iSelection) {
1 -> println(if (iRandom == 3) "승" else "패")
2 -> println(if (iRandom == 1) "승" else "패")
3 -> println(if (iRandom == 2) "승" else "패")
else -> println("1, 2, 3 중 하나를 입력해주세요.")
}
}
}
1(가위), 2(바위), 3(보) 입력 : 2
사용자 : 바위
컴퓨터 : 바위
비김