Annoyer
Mr & Ms Pac-Man
Ặc thôi, học càng sớm càng tốt, chắc học.NET quá
học để làm gì ?
Ặc thôi, học càng sớm càng tốt, chắc học.NET quá
Ặc thôi, học càng sớm càng tốt, chắc học.NET quá
Python là ngôn ngữ mở, hoàn toàn free rất thích hợp cho người mới bắt đầu lập trình. Mọi thông tin và hướng dẫn có thể tìm thấy miễn phí tại python.org. Nếu tiếng Anh không tốt có thể ghé qua trang của cộng đồng Python Việt tại vithon.org. Tuy nhiên chỉ mới có hướng dẫn cho Python 2.5 là tiếng Việt. Nếu bạn muốn đọc hướng dẫn Python 3 thì chịu khó xem tiếng Anh và tra từ thôi. Chúc bạn vui.

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <set>
#include <string>
using namespace std;
//return a random number in range [min,max] inclusive
inline int randrange(int min, int max) {return rand()%(max-min+1) + min;}
void save(const string&, int);
int main()
{
srand(time(0));
const int minVal = 1;
const int maxVal = 4; //1-4 for easy win
const int prizes[] = {50,20,-10}; //set your prizes here
const int ncount = sizeof(prizes)/sizeof(*prizes);
const int winScore = 300; //set your win score here
int score = 100; //start with 100
int num[ncount];
set<int> counter;
int prize;
string dummy;
while (score>0 && score<winScore)
{
//wait until player hits enter
cout << "Hit enter to play! ";
getline(cin, dummy);
//generate 3 random numbers
for (int i=0; i<ncount; ++i) num[i] = randrange(minVal,maxVal);
//decide win prize
counter.clear();
counter.insert(num, num+ncount);
prize = prizes[counter.size()-1];
//add prize to total
score += prize;
//output
for (int i=0; i<ncount; ++i) cout << num[i] << " ";
prize < 0 ? cout << "\nYou lose " << -prize
: cout << "\nYou win " << prize;
cout << "\nScore: " << score << endl << endl;
}
if (score >= winScore)
{
cout << "You win! Please enter your name: ";
getline(cin, dummy);
save(dummy,score);
}
else cout << "You lose! Better luck next time!\n";
cin.sync();
cin.get();
}
void save(const string& name, int score)
{
fstream hiscore("highscores.txt", ios::in|ios::out|ios::app);
if (!hiscore) {cout<<"Cannot open file\n"; exit(1);}
const int limit = 10;
string lines[limit];
string line;
int i = 0;
//save
hiscore << score << " " << name << endl;
cout << "Your score has been saved!\n\n";
//print hiscore content in reverse order, limit 10
hiscore.clear();
hiscore.seekg(0);
while (getline(hiscore,line)) { if (line!="") lines[i++]=line; i%=limit; }
--i;
for (int j=0; j<limit; ++j) cout << lines[(i-j+limit)%limit] << endl;
hiscore.close();
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
public class QuaySo {
public static Scanner keyboard = new Scanner(System.in);
public static Random rand = new Random();
public static int random(int min, int max) {
return rand.nextInt(max - min + 1) + min;
}
public static void save(String name, int score) {
//save
try {
FileWriter ofstream = new FileWriter("highscores.txt",true);
BufferedWriter out = new BufferedWriter(ofstream);
out.write(score + " " + name + "\n");
out.close();
System.out.print("Your score has been saved!\n\n");
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
//print hiscore content in reverse order, limit 10
final int limit = 10;
try {
FileReader ifstream = new FileReader("highscores.txt");
BufferedReader in = new BufferedReader(ifstream);
String lines[] = new String[limit];
String line;
int i = 0;
while ((line = in.readLine()) != null) {
lines[i++]=line; i%=limit;
}
for (int j=1; j<limit; ++j) {
if ((line = lines[(i-j+limit)%limit]) != null) {
System.out.println(line);
}
else {
break;
}
}
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
public static void main(String[] args) {
final int minVal = 1;
final int maxVal = 4;
final int[] prizes = {50,20,-10};
final int winScore = 300;
int score = 100;
int num[] = new int[prizes.length];
Set<Integer> counter = new HashSet<>();
int prize;
String temp;
while (score>0 && score<winScore) {
//wait until player hits enter
System.out.print("Hit enter to play! ");
keyboard.nextLine();
//generate 3 random numbers
for (int i=0; i<prizes.length; ++i) {
num[i] = random(minVal,maxVal);
}
//decide win prize
counter.clear();
for (int i : num) {
counter.add(i);
}
prize = prizes[counter.size()-1];
//add prize to total
score += prize;
//output
for (int i : num) {
System.out.print(i + " ");
}
if (prize < 0) {
System.out.print("\nYou lose " + (-prize));
}
else {
System.out.print("\nYou win " + prize);
}
System.out.print("\nScore: " + score +"\n\n");
}
if (score >= winScore) {
System.out.print("You win! Please enter your name: ");
temp = keyboard.nextLine();
save(temp,score);
}
else {
System.out.print("You lose! Better luck next time!\n");
}
System.out.print("\nHit enter to end program...");
keyboard.nextLine();
} //main
}