2010年12月10日 星期五

Minesweeper

//************** mine.h **************
#include <iostream>
#include
<ctime>
using namespace std;

#define NOT_SHOW 'X'

/*
//==================class cell====================
//Obsolete, too slow
class cell
{
private:
bool _is_mine;
//bool _is_show;
int _neighbors;
public:
cell(void): _is_mine(0), _neighbors(0) {}
void set_isMine(bool in) {_is_mine = in;}
//void isShow(bool in) {_is_show = in;}
void neighborPlus(void) {_neighbors++;}
bool get_isMine(void) {return _is_mine;}
//bool get_isShow(void) {return _is_show;}
int get_neighbors(void) {return _neighbors;}
virtual ~cell(void);
};
*/

//================class mineGame===================
class mineGame
{
private:
int _width;
int _height;
int _mine_num; // mine#
int _remain; // number of ~mine cell, default=(_width*_height)-_mine_num, user wins when it reaches to 0
int _c_x, _c_y; // user current selected x,y
bool **_show_board; // 0: hide(default), 1: show
int **_mine_board; // cell board: -1:mine, 0:empty , 1~8:amount of neighbors
public:
mineGame(
void): _width(0), _height(0), _mine_num(0), _remain(0), _c_x(-1), _c_y(-1), _show_board(NULL),_mine_board(NULL) {}
mineGame(
int width, int height, int mine_num);
void createGame(void);
void setGame(void);
void setMine(void);
void playGame(void);
void drawGame(void);
int userSelectEvent(void); //return value: continue=0, quit=1
int currentSelectEvent(void); //return value: continue=0, win=1,lose=-1
void gameOverEvent(int in); //input value: win=1,lose=-1
int revealBoard(const int x, const int y);
void drawCell(const int x, const int y);
int checkNeighbors(const int x, const int y);

virtual ~mineGame(void);
};

//mineGame
mineGame::mineGame(int width, int height, int mine_num)
{
//initial parameters
_width = width;
_height
= height;
_mine_num
= mine_num;
_remain
=(_width*_height)-_mine_num;
//initiallize
createGame();
playGame();
}

//**create game**//
void mineGame::createGame()
{
setGame();
setMine();

}

//set game
void mineGame::setGame()
{
//create board & show board
_mine_board = new int*[_width];
_show_board
= new bool*[_width];
for (int i=0;i<_height;i++)
{
_mine_board[i]
= new int[_height];
_show_board[i]
= new bool[_height];
}
//initialize board & show board
for (int j=0; j<_height; j++)
for (int i=0; i<_width; i++)
{
_mine_board[i][j]
= 0;
_show_board[i][j]
= false; ////**************
}

}

//set mine
void mineGame::setMine()
{
int set_mine = _mine_num;
int count = 1;
int s_x,s_y;
srand ( time(NULL) );
//set mine to cell
cout << "Set mine.\n";
while (set_mine)
{
s_x
= rand() % _width;
s_y
= rand() % _height;
if (_mine_board[s_x][s_y]==0)
{
//cout << "Set mine #" << count << "\n";
_mine_board[s_x][s_y]=-1;
set_mine
--;
count
++;
}
}
cout
<< "Set mine done.\n";
cout
<< "Check neighbors.\n";
//check # of mines of neighbors on each cell and fill the # of neighbors
for (int j=0; j<_height; j++)
for (int i=0; i<_width; i++)
checkNeighbors(i,j);
cout
<< "Check neighbors done.\n";
}

//**play game**//
void mineGame::playGame()
{
int game_over=-2;
drawGame();
while(1)
{
//if userSelectEvent()==-1, user quit game
if (userSelectEvent()==-1)
break;

//user play
//if game_over=0, continue play
if ((game_over=currentSelectEvent())==0)
{
drawGame();
continue;
}
//else user win/lose
else
{
gameOverEvent(game_over);
break;
}
}
}
//draw game
/*

Example: width=15,height=15
then it will diplay a board of size (15+3)*(15+3) like below

012346789ABCDE
/--------------/
0|***************|
1|***************|
2|***************|
3|***************|
4|***************|
5|***************|
6|***************|
7|***************|
8|***************|
9|***************|
A|***************|
B|***************|
C|***************|
D|***************|
E|***************|
/--------------/

*/
void mineGame::drawGame()
{
//draw outter edge
for (int j=-2; j<_height+1; j++)
for (int i=-2; i<_width+1; i++)
{
//draw first line
if (j==-2)
if (i==-2 || i==-1 || i==_width)
printf(
" ");
else if (i<10)
printf(
"%d",i);
//print A~Z if i>9
else printf("%c",i+55);
//draw second line
else if (j==-1 || j==_height)
if (i==-2)
printf(
" ");
else if (i==-1 || i==_width)
printf(
"/");
else printf("-");
else
if (i==-2)
if (j<10)
printf(
"%d",j);
else printf("%c",j+55);
else if (i==-1 || i==_width)
printf(
"|");
//else printf("*");
else drawCell(i,j);
if (i==_width)
printf(
"\n");

}


}

void mineGame::drawCell(const int x, const int y)
{
if (_show_board[x][y]==true)
if (_mine_board[x][y]==-1)
printf(
"*");
else if (_mine_board[x][y]==0)
printf(
" ");
else printf("%d",_mine_board[x][y]);
else printf("%c",NOT_SHOW);
}


//check number of neighbors
int mineGame::checkNeighbors(const int x, const int y)
{
if (_mine_board[x][y]==-1)
return 0;
for (int i=x-1; i<x+2; i++)
for (int j=y-1; j<y+2; j++)
{
if (i<0 || j<0 || i>_width-1 || j>_height-1)
continue;
if (_mine_board[i][j]==-1)
_mine_board[x][y]
++;
}
return 0;
}

//check cell
int mineGame::userSelectEvent()
{
char usr_x = 255, usr_y = 255;
char usr_p = 255;
_c_x
=-1;
_c_y
=-1;
bool have_enter_b4=false;
bool repeat_aixs=false;
bool illegal_axis=false;
while (1)
{
//if have enter before
if (have_enter_b4)
{
//if illegal axis
if (illegal_axis)
{
illegal_axis
=false;
cout
<< "\nQuit the game? (y to quit)\n";
cin
>> usr_p;
cin.ignore(
256,'\n'); //prevent user enter string
if (usr_p=='y' || usr_p=='Y')
return -1;
}
//if legal axis but have visited before
else if (_show_board[_c_x][_c_y])
{
cout
<< "\nThis axis have been selected before.";
repeat_aixs
=true;
}
//if legal and not been visited
else return 0;
}
//if first enter
else have_enter_b4=true;

//user enter axis
cout << "\nPlease enter cell axis:\n";
cin
>> usr_y;
cin.ignore(
256,' '); //prevent user enter string
cin >> usr_x;
cin.ignore(
256,'\n'); //prevent user enter string
/*
cout << "usr_y\"" << usr_y << "\"\n" ;
cout << "usr_x\"" << usr_x << "\"\n" ;
cout << "usr_p\"" << usr_p << "\"\n" ;
*/
if (usr_y==NULL || usr_x==' ')
{
cout
<< "here\n";
illegal_axis
=true;
usr_y
=usr_x=0;
continue;
}
//exam the axis
if (10>_width || 10>_height)
{
if ((47<usr_x && 47<usr_y && 58>usr_x && 58>usr_y))
{
_c_x
= usr_x-48;
_c_y
= usr_y-48;
continue;
}
illegal_axis
=true;
repeat_aixs
=false;
cout
<< "Wrong input.";
continue;
}
else
{
if (47<usr_x && 58>usr_x)
if (47<usr_y && 58>usr_y)
{
_c_x
= usr_x-48;
_c_y
= usr_y-48;
continue;
}
else if (64<usr_y && 64+_height-9>usr_y)
{
_c_x
= usr_x-48;
_c_y
= usr_y-55;
continue;
}
else if (96<usr_y && 96+_height-9>usr_y)
{
_c_x
= usr_x-48;
_c_y
= usr_y-87;
continue;
}
if (64<usr_x && 64+_width-9>usr_x)
if (47<usr_y && 58>usr_y)
{
_c_x
= usr_x-55;
_c_y
= usr_y-48;
continue;
}
else if (64<usr_y && 64+_height-9>usr_y)
{
_c_x
= usr_x-55;
_c_y
= usr_y-55;
continue;
}
else if (96<usr_y && 96+_height-9>usr_y)
{
_c_x
= usr_x-55;
_c_y
= usr_y-87;
continue;
}
if (96<usr_x && 96+_width-9>usr_x )
if (47<usr_y && 58>usr_y)
{
_c_x
= usr_x-87;
_c_y
= usr_y-48;
continue;
}
else if (64<usr_y && 64+_height-9>usr_y)
{
_c_x
= usr_x-87;
_c_y
= usr_y-55;
continue;
}
else if (96<usr_y && 96+_height-9>usr_y)
{
_c_x
= usr_x-87;
_c_y
= usr_y-87;
continue;
}
//user wrong input
illegal_axis=true;
repeat_aixs
=false;
cout
<< "Wrong input.";
continue;
}
}

}

int mineGame::currentSelectEvent()
{
//if current x,y is a mine
//return -1 to over the game
if (_mine_board[_c_x][_c_y]==-1)
return -1;

//if current x,y is # of neighbors
//set show=true, _remain-1
if (_mine_board[_c_x][_c_y])
{
_show_board[_c_x][_c_y]
=true;
_remain
--;
}
//if current x,y is empty
//reveal its neighbors
if (_mine_board[_c_x][_c_y]==0)
{
revealBoard(_c_x, _c_y);
}
return _remain?0:1;
}

//gameOverEvent
void mineGame::gameOverEvent(int in)
{

for (int j=0; j<_height; j++)
for (int i=0; i<_width; i++)
{
_show_board[i][j]
= true; ////**************
}
drawGame();
if (!in)
cout
<< "You win!\n";
else cout << "You lose! Try again.\n";
}

//reveal board
//If current selected cell is empty(ie. there are no mines in neighbors), set neighbors to show if it's empty too.
int mineGame::revealBoard(const int x, const int y)
{
//If already shown or is mine, retrun
if (_show_board[x][y] || _mine_board[x][y]==-1)
return 0;

//Set show if not show, _remain-1
_show_board[x][y]=true;
_remain
--;
//if current cell is empty, show its neighbors if they are empty too
if (_mine_board[x][y]==0)
for (int i=x-1; i<x+2; i++)
for (int j=y-1; j<y+2; j++)
{
if (i<0 || j<0 || i>_width-1 || j>_height-1)
continue;
revealBoard(i,j);
}
return 0;
}


//************** mine.cpp **************
#include <iostream>
#include
<string>
#include
"mine.h"

using namespace std;
/*
cell::~cell(void)
{
}
*/

mineGame::
~mineGame(void)
{
}


void main()
{
int width=0, height=0, mine=0;
char level=0;
int c_x=-1,c_y=-1;
char play='y';
while (play=='y' || play=='Y')
{
//user select play and level
cout << "Play a new game? (Enter y to play, other to exit.)\n";
cin
>> play;
cin.ignore(
256,'\n'); //prevent user enter string
if (play=='y' || play=='Y')
{
cout
<< "Please enter game level: (1~3)\n";
cin
>> level; //prevent user enter string
cin.ignore(256,'\n');
if (level=='1' || level=='2' || level=='3')
{
switch (level)
{
case '1':
width
=9, height=9, mine=10;
break;
case '2':
width
=12, height=12, mine=18;
break;
case '3':
width
=16, height=16, mine=40;
break;
}

}
else
{
cout
<< "Level not exist!\n";
continue;
}
}
else break;

//start mine game
mineGame mineGame(width, height, mine);

}
cout
<< "Thank you for playing.\n";
system(
"pause");
return;
}

2010年9月19日 星期日

簡易猜數字

//newGame.h//
#pragma once

class newGame
{
public:
newGame(void);
virtual void gameStart(void) = 0;
virtual void setGame(int nums) = 0;
virtual void inGame(void) = 0;
virtual ~newGame(void){}
};

class guessNumberGame : public newGame
{
public:
guessNumberGame(void);
void gameStart(void);
void setGame(int nums);
void inGame(void);
void examNum(void);
virtual ~guessNumberGame(void);
private:
int _nums;
int _A,_B;
char* _secretNum;
char* _guessNum;

};

//newGame.cpp//
#include
#include
#include "newGame.h"

newGame::newGame(void)
{
}

guessNumberGame::guessNumberGame(void)
{
gameStart();
}

guessNumberGame::~guessNumberGame(void)
{
}

void guessNumberGame::gameStart(void)
{
int nums;
printf("Enter a number to start the game:(4~6 to play, 0 to exit.)\n");
while (scanf("%d",&nums))
{
if (nums==0) break;
if (nums<4 || nums>6)
{
printf("Wrong input!\n");
printf("Enter a number to start the game:(4~6 to play, 0 to exit.)\n");
continue;
}
_nums=nums;
setGame(_nums);
inGame();
printf("Enter a number to start the game:(4~6 to play, 0 to exit.)\n");
}
printf("Exit the game. Thank you for playing.\n");
system("pause");
}

void guessNumberGame::setGame(int _nums)
{
_secretNum = new char[_nums];
_guessNum = new char[_nums];
int arr[10];
int j=0,tmp;
srand(time(NULL));
for (int i=0;i<10;i++)
{
arr[i]=i;
}
while(j<_nums)
{
tmp = rand() % 10;
if (arr[tmp]>-1)
{
_secretNum[j]=arr[tmp];
arr[tmp]=-1;
j++;
}
}
printf("secretNum = ");
for (int i=0;i<_nums;i++)
printf("%d",_secretNum[i]);
printf("\n");
}

void guessNumberGame::inGame(void)
{
int chance = 10;
for (int i=0;i {
printf("Enter your guess(%d):",i+1);
flushall();
for (int j=0;j<_nums;j++)
{
scanf("%c",(_guessNum+j));
}
examNum();
printf("%dA%dB\n",_A,_B);
if (_A == _nums)
{
printf("You win!\n");
break;
}
//for (int j=0;j<_nums;j++)
//{
// printf("%c",_guessNum[j]);
//}
}
}

void guessNumberGame::examNum(void)
{
_A = _B = 0;
for (int i=0;i<_nums;i++)
{
for (int j=0;j<_nums;j++)
{
if (_secretNum[i]+48==_guessNum[j])
{
_B++;
if (i==j) _A++;
continue;
}
}
}
}

int main()
{
guessNumberGame game;
return 0;
}

2010年9月1日 星期三

ACM102

資源回收題
題目from lucky貓
http://www.tcgs.tc.edu.tw/~sagit/luckycat/q102.htm

code:
#include
using namespace std;

#define MAX_BIN 3 //max number of bins
#define MAX_TYPE 3 //max number of type

enum bottleType {B,C,G};

class bin{
public:
bin(): _type(0) {}
char getType(){return _type;}
void modifyType(char c){_type=c;}
//void setBottle(int in,int i){}
private:
char _type;
int _stock[MAX_TYPE]; //bottle stock (order:BCG)


friend class binWorld;
};

class binWorld{
public:
binWorld();
char returnType(int j);
void setBottle(int i, int j, int in);
void print(); //print result
void searchMax(); //search largest bottle numbers in each bin (under condition of _type not been defined)
void moveBottle(); //move bottles

private:
bin* _binPtr;
int _move_count;
int _max;
int _bin_num;
int _stock_num;
bool* _isDefine;
};



binWorld::binWorld(){
_binPtr= new bin[MAX_BIN];
_isDefine= new bool[MAX_TYPE];
_move_count=0;
_max=0;
_bin_num=-1;
_stock_num=-1;
for (int j=0;j}

char binWorld::returnType(int j){
if (j==1) return 'C';
else if (j==0) return 'B';
else return 'G';

}

void binWorld::setBottle(int i, int j, int in){
if (j==1) (_binPtr+i)->_stock[G]=in;
else if (j==0) (_binPtr+i)->_stock[B]=in;
else (_binPtr+i)->_stock[C]=in;
//(_binPtr+i)->_stock[returnType(j)]=in;
}

void binWorld::print(){
for (int i=0;i_type);
printf(" %d\n",_move_count);
}

//modify max & type number
void binWorld::searchMax(){
_max=-1;
for (int i=0;i if (!(_binPtr+i)->_type) {
for (int j=0;j if (_max<(_binPtr+i)->_stock[j] && *(_isDefine+j)==false){
_max=(_binPtr+i)->_stock[j];
_bin_num=i;
_stock_num=j;
}
}
}
}
(_binPtr+_bin_num)->_type=returnType(_stock_num);
*(_isDefine+_stock_num)=true;
}

void binWorld::moveBottle(){
for (int i=0;i if (i==_bin_num) continue;
_move_count+=(_binPtr+i)->_stock[_stock_num];
}
}

int main(){
char bottles[30];
int in;
FILE *f;
fopen_s(&f,"ACM102.txt","rt");
if (f==NULL) {printf("Error open file."); return -1;}
while (!feof(f)){
binWorld binWorld;
//load input (one line)
printf("Input:");
for (int i=0;i for (int j=0;j fscanf(f,"%d",&in);
binWorld.setBottle(i,j,in);
printf("%d ",in);
}
}

for (int i=0;i binWorld.searchMax();
//binWorld.tprint();
binWorld.moveBottle();
}
printf("\nOutput:");
binWorld.print();
}
fclose(f);
}

2010年8月30日 星期一

ACM101

參考了一下網路上前輩的方法
決定採用雙向linklist,另外用一個array來記錄各個block是否有存放東西

題目翻譯連結(lucky貓的ACM園地)
http://www.tcgs.tc.edu.tw/~sagit/luckycat/q101.htm

code:

#include
#include
using namespace std;

#define N_MAX 24

class linkList{
int obj;
linkList *next;
linkList *prev;
public:
linkList(): obj(0), next(0), prev(0) {}
void modifyObj(int n){
obj=n;
}
int printObj(){
return obj;
}

friend class blockWorld;
};

class blockWorld{
public:
//initialization
blockWorld(int);
blockWorld(): blockMap(0), _n(0) {}

//destructor
~blockWorld() {};

//function
void print();
void add(int , int);
void clear(int);
bool isSame(int ,int);

private:
//variable
unsigned _n;
linkList* blockMap; //pointer array to each block
bool* blockExist; //array of size n, if blockExist[i]=0:empty block, and vis versa.
};

blockWorld::blockWorld(int n) {
this->_n=n;
blockMap = new linkList[n];
blockExist = new bool[n];
//initialization
for (int i=0;i<_n;i++){
*(blockExist+i)=1;
(blockMap+i)->modifyObj(i);
}
}

void blockWorld::print(){
for (int i=0;i<_n;i++){
printf("%d: ",i);
if (*(blockExist+i)) {
linkList *tmp;
tmp=blockMap+i;
printf("%d ",tmp->printObj());
while (tmp->next){
tmp=tmp->next;
printf("%d ",tmp->printObj());
}
}
printf("\n");
}
}

void blockWorld::add(int a, int b){
if ((blockMap+a)->prev==NULL){
*(blockExist+a)=0;
}
else {
(blockMap+a)->prev->next=NULL;
}
linkList *tmp;
tmp=blockMap+b;
while (tmp->next) tmp=tmp->next; //find b's last tail
(blockMap+a)->prev=tmp;
tmp->next=(blockMap+a);
}

//clear a's tail, put them back to original position, eg:i put to i and so on
void blockWorld::clear(int a){
linkList *tmp1,*tmp2;
tmp1=blockMap+a;
while (tmp1->next){
tmp2=tmp1->next;
tmp1->next=NULL;
tmp2->prev=NULL;
*(blockExist+tmp2->obj)=1; //set blockExist i to true
tmp1=tmp2;
}
}

//Is a and b in the same block?
bool blockWorld::isSame(int a,int b){
linkList *tmp;
tmp=blockMap+a;
//search a's next
while (tmp->next) {
if (tmp->next->obj==b) return true;
tmp=tmp->next;
}
//search a's previous
tmp=blockMap+a;
while (tmp->prev) {
if (tmp->prev->obj==b) return true;
tmp=tmp->prev;
}
return false;
}



int main(){
FILE *f;
int in,a,b;
char cm1[10],cm2[10];
f=fopen("ACM101.txt","rt");
if (f==NULL){
printf("No such file.\n");
return -1;
}

//create blockWorld


while (fscanf(f,"%d",&in)){
if (in<1) {
printf("Error: Input must larger than 1.\n");
continue;
}
printf("%d blockWorld\n",in);
blockWorld blockWorld(in);
fscanf(f,"%s",&cm1);
while (strcmp(cm1,"quit")) {
fscanf(f,"%d %s %d\n",&a,&cm2,&b);
printf("%s %d %s %d\n",cm1,a,cm2,b);
if (blockWorld.isSame(a,b)) {
fscanf(f,"%s",&cm1);
continue;
}
if (!strcmp(cm1,"move")) {
blockWorld.clear(a);
}
if (!strcmp(cm2,"onto")){
blockWorld.clear(b);
}
blockWorld.add(a,b);
fscanf(f,"%s",&cm1);
}
printf("%s\n",cm1);
printf("======================================================\nResult\n");
blockWorld.print();
printf("======================================================\n");
if (feof(f)) break;
}
fclose(f);
return 0;
}

2010年8月2日 星期一

個人模型作品

自己製作的模型作品

作品一:場景製作,以縣政府為景的場景建模








作品二:人物製作






2010年8月1日 星期日

團隊作品

於sony eschool與美術團隊合作的作品

作品一:Q版人物動作
模型皆由別人製作,我負責動作部份

1.待機


2.跑步



3.攻擊


4.滑倒


5.死亡




作品二: 遊戲開頭動畫製作
專案遊戲開頭動畫,我主要負責的部分為影片剪接,雷電子彈特效
角色動作,及部分素材的rendering
使用軟體為:MAYA、AE、PREMIERE等