(JavaScript) 유저 vs 컴퓨터 가위바위보 게임 만들기

정세비·2021년 5월 17일
0

test

목록 보기
3/13
post-thumbnail
  1. 모든 rock, scissors, paper 는 소문자로 작성될 것
  2. rock, scissors, paper 외의 글자 입력시 'Error' 표시
  3. 컴퓨터가 선택하는 숫자는 0~2의 랜덤숫자이며, 각 숫자마자 r, s, p 설정할 것
  4. 승패에 따라 메시지 표시하기 (비김, 유저가 승리, 컴퓨터가 승리)
  5. 만약 유저가 bomb를 입력하면 무조건 유저 승리
  6. <출력값>
    • 유저 : rock
    • 컴퓨터 : (rock , scissors, paper 중 랜덤값)
    • 00가 승리
const userchoice = userinput => {
	userinput = userinput.toLowerCase();
    if (userinput === 'rock' || userinput === 'scissors' || userinput === 'paper' || userinput === 'bomb') {
    return userinput;
   } else {
	   console.log('Error');
   }
}

const computerchoice = () => {
	number = Math.floor(Math.random() * 3);
    if (number === 0) {
    	return 'rock';
    } else if (number === 1) {
    	return 'scissors'
    } else { 
    	return 'paper'
    }
}   
    const winner = (user, computer) => {
    	if (user === computer) {
        	return '비김'
        }
        if (user === 'rock') {
        	if(computer === 'scissors') {
            	return '유저가 승리';
           } else {
           		return '컴퓨터가 승리';
           }
        }
        if (user === 'scissors') {
        	if(computer === 'paper') {
            	return '유저가 승리';
           } else {
           		return '컴퓨터가 승리';
           }
        }    
       if (user === 'paper') {
        	if(computer === 'rock') {
            	return '유저가 승리';
           } else {
           		return '컴퓨터가 승리';
           }
       }         
       if (user === 'bomb') {
        		return '유저가 승리';
       }
    } 
    
    const play = () => {
    	const user = userchoice('rock');
      const computer = computerchoice();
      console.log(`유저 : ${user}`);
      console.log(`컴퓨터 : ${computer}`);
      console.log(winner(user, computer));
    }
    
    play();
profile
파주

0개의 댓글