Coding waktu aku semester 1 study C++
walo dapet nilai buruk tapi gak papa lah aku post di sini ^^
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#define NUMROWS 4
class Move
{
public:
int from;
int to;
Move(int a, int b)
{ from = a; to = b;}
};
static void display(int*);
static int Sum(int*);
void play();
void initialize(int*, Move&);
int recur(int*, int);
int evaluate(int*);
int skill = 7;
int main()
{
int skill_choice;
cout << "********************************************************************************\n";
cout << "*------------------------------------------------------------------------------*\n";
cout << "* Ini adalah game classic dari Nim. *\n";
cout << "* Oleh Kelompok yang terhebat : *\n";
cout << "* - Wahyudi (09018121) *\n";
cout << "* - Yunita tri hernawati (09018135) *\n";
cout << "* - Rosary indah dewi (09018168) *\n";
cout << "* Setidaknya yang kami tahu. Ini permainan untuk memaksa anda untuk berfikir *\n";
cout << "* siapa yang mengambil potongan terahir akan kalah *\n";
cout << "* dari potongan-potongan dari SATU DAN HANYA SATU baris per putaran. *\n";
cout << "* Kemudian komputer akan berfikir dan mengambil giliranya *\n";
cout << "* game ini sangatlah sulit.. selamat mencoba. *\n";
cout << "*------------------------------------------------------------------------------*\n";
cout << "********************************************************************************\n\n\n";
do{
cout << "Pilih level:\n";
cout << "(0) Pemula\n";
cout << "(1-3) Sedang\n";
cout << "(4) sangat pintar (tapi akan membutuhkan waktu untuk berfikir)\n";
cin >> skill_choice;
} while(skill_choice >4 || skill_choice < 0);
skill = skill_choice*2 +1;
while(1){
int again;
play();
cout << "\n\n\nmencoba kembali:\n";
cout << "(1) Ya\n(2) Tidak\n";
cin >> again;
if(again != 1) break;
}
}
void play()
{
int sum;
int row[4] = {0, 5, 7, 9};
int whichrow, howmany;
display(row);
while(1)
{
cout << "Baris yang akan Anda ambil dari potongan-potongan (1-3): ";
cin >> whichrow;
while(whichrow < 1 || whichrow > 3 || row[whichrow] <=0)
{
cout << "Tidak cocok, mohon ulang kembali (1-3): ";
cin >> whichrow;
}
sum = Sum(row);
cout << "Berapa banyak yang akan Anda ambil dari baris " << whichrow;
cout << "? ";
cin >> howmany;
while(sum - howmany < 1 || row[whichrow] - howmany < 0 ||
howmany < 1)
{
cout << "Anda mencoba mengambil terlalu banyak atau terlalu sedikit!\n";
cout << "Ambil lagi: ";
cin >> howmany;
}
row[whichrow] = row[whichrow] - howmany;
display(row);
if(Sum(row) == 1)
{
cout << "Kamu menang dan selamat anda akan mendapatkan rasa senang (ha ha)!!!";
return;
}
Move move(0, 0);
initialize(row, move);
cout << "Giliranku, aku akan mengambil " << move.to << " Dari baris ";
cout << move.from << "." << endl;
row[move.from] = row[move.from] - move.to;
display(row);
if(Sum(row) == 1)
{
cout << "Saya menang!!! Rasahin loe !\n";
return;
}
}
return;
}
static void display(int* rows)
{
rows++;
cout << "|--------------------------------------------------------|\n\n";
for(int i = 1; i < NUMROWS; i++)
{
if(i==1) cout << " ";
if(i==2) cout << " ";
for(int j = 1; j <= *rows; j++)
cout << " |";
rows++;
cout << "\n\n";
}
cout << "|--------------------------------------------------------|\n\n";
}
static int Sum(int* row )
{
int total = 0;
row++;
for(int i = 1; i < NUMROWS; i++)
{
total+=*row;
row++;
}
return total;
}
void initialize(int* array, Move& move)
{
Move& m = move;
int best = -1, depth = 1;
int row[4];
for(int i = 0; i < 4; i++)
{
row[i] = *array;
array++;
}
array= array-3;
for(int j = 1; j <= 3; j++)
{
int thisrow = row[j];
for(int i = 1; i <= thisrow; i++)
{
int value;
row[j]--;
value = recur(row, depth);
if(value > best)
{
best = value;
m.from = j;
m.to = i;
}
}
row[j] = *array;
array++;
}
}
int recur(int* array, int depth)
{
int best = -1;
int worst = 101;
int row[4];
for(int i = 0; i < 4; i++)
{
row[i] = *array;
array++;
}
if(Sum(row) == 0)
{
cout << "Pergerakan dilarang\n";
return 50;
}
if(Sum(row) == 1)
{
if(depth%2 == 1)
return 100;
else return 0;
}
array= array-3;
if(depth == skill)
{
int val = evaluate(row);
if(depth%2 == 1)
return 100-val;
else
return val;
}
for(int j = 1; j <= 3; j++)
{
int thisrow = row[j];
for(int i = 1; i <= thisrow; i++)
{
int value;
row[j]--;
if(Sum(row)== 0) break;
value = recur(row, depth+1);
if(value > best)
{
best = value;
}
if(value < worst)
{
worst = value;
}
}
row[j] = *array;
array++;
}
if(depth%2 == 1)
{
return worst;
}
else
{
return best;
}
}
int evaluate(int* array)
{
int total, high=0, low=10;
int row[4];
for(int i = 0; i < 4; i++)
{
row[i] = *array;
array++;
}
total = Sum(row);
for(int i = 1; i <=3; i++)
{
if(row[i] < low)
low = row[i];
if(row[i] > high)
high = row[i];
}
if(total == 1)
return 100;
if(high==1 && low ==1)
return 2;
if(total == 2)
return (99);
if(high==2)
{
if(total==4)
return total;
else return(100-total);
}
if(low==0)
{
if(total==(2*high))
return total;
else return 100-(total*2);
}
if(high==3)
{
if (total==6) return 10;
else return(100-total);
}
return 50;
}
" itu diatas coding game di semester 1 "
maaf agak tidak enak dilihat :D
Best view Wahyudi with Firefox 3+ & minimum monitor resolusion 1280×1024px
Belajar © Otak atik Blog ^^ Jadi maap kalo ada kekurangan