2024-02-14

妻子的程式第一個作品

某日妻子突然對程式編寫有興趣,因此向我查詢過一些意見後
便嘗試使用 C++ 編寫一個猜包剪揼程式

妻子自發學習了一些基礎程式語言的語法
例如 原始類型(Primitive Type) 、 判別式 (Condition) 、 迴圈 (Looping) 、 陣列 (Array)
然後教導妻子如何有效地從網上尋找合適的資料製作程式

最後妻子自己製作了一個可以讓使用輸入包(paper)、剪(scissors)、揼(rock)與電腦(隨機)對玩的程式

#include <iostream>
#include <string>
#include <random>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main() {
	string rock = "rock";
	string paper = "paper";
	string scissors = "scissors";
	string x;
	string y;
	cout << "(rock, paper, scissors) Shoot! (Player): ";
	cin >> x;
	string CPU[3] = {"rock", "paper", "scissors"};
	unsigned seed;
	seed = (unsigned)time(NULL);
	srand(seed); 
	y = CPU[rand() % 3];
	cout << "CPU: " <<y <<"\n";
	if ( x == "rock" && y == "scissors") {
		cout << "Player wins!";
	} else if ( x == "paper" && y == "scissors") {
		cout << "CPU wins!";
	} else if ( x == "scissors" && y == "scissors") {
		cout << "Fair play!";
	} else if ( y == "rock" && x == "scissors") {
		cout << "CPU wins!";
	} else if ( y == "paper" && x == "scissors") {
		cout << "Player wins!";
	} else if ( x == "paper" && y == "paper") {
		cout << "Fair play!";
	} else if ( x == "paper" && y == "rock") {
		cout << "Player wins!";
	} else if ( x == "rock" && y == "paper") {
		cout << "CPU wins!";
	} else if ( x == "rock" && y == "rock") {
		cout << "Fair play!";
	} else if ( x == "scissors" && y == "scissors") {
		cout << "Fair play!";
	} else {
		cout << "ERROR!";
	}
	return 0;
}

妻子用4小時自學編寫這個程式,妻子都很滿意結果,覺得編寫程式很有趣

沒有留言:

發佈留言