作者 主題: parallel arrays  (閱讀 2499 次)

0 會員 與 1 訪客 正在閱讀本文。

daniel212hk

  • 可愛的小學生
  • *
  • 文章數: 1
    • 檢視個人資料
parallel arrays
« 於: 2012-10-22 14:39 »
剛剛寫了這份功課,但現在想把兩位玩家的資料用parallel arrays存儲...我該怎樣改?
3Q


代碼: [選擇]
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <iostream>
#include <string>
using namespace std;

const string empty_string = "";
const int winner_points = 16;

//data player 0
int points0;
string name0 = empty_string;
bool lose_turn0;
bool extra_turn0;
bool winner0;
 
//data player 1
int points1;
string name1 = empty_string;
bool lose_turn1;
bool extra_turn1;
bool winner1;

//constructors
void initPlayer(int player_id, const string& name);

//accessors
bool getWinner(int player_id);
int getPoints(int player_id);
string getName(int player_id);
bool getLoseTurn(int player_id);
bool getExtraTurn(int player_id);

//mutators
void setWinner(int player_id, bool winner);
void setPoints(int player_id, int points);
void setName(int player_id, const string& name);
void setLoseTurn(int player_id, bool lose_turn);
void setExtraTurn(int player_id, bool extra_turn);

//custom functions
void computePoints(int player_id, int head_count);
void computeStatus(int player_id, int head_count);
int selectFirstPlayer();
void toss2Coins(int& coin_face1, int& coin_face2);
int selectNextPlayer(int player_id);
int otherId(int player_id);

int main()
{
srand(unsigned(time(NULL)));
//srand(1);
initPlayer(0, "Zeno");
initPlayer(1, "Orestes");

int player_id = selectFirstPlayer();

do
{
int face1, face2;
toss2Coins(face1, face2);
int head_count = face1 + face2;
computePoints(player_id, head_count);
computeStatus(player_id, head_count);
cout << getName(player_id) << " tosses " << head_count << " heads" << endl;
describe(player_id);
player_id = selectNextPlayer(player_id);
}
while(!getWinner(player_id));
system("pause");
return 0;
}
void computeStatus(int player_id, int head_count)
{
if(head_count == 0)
{
setLoseTurn(player_id, true);
setExtraTurn(player_id, false);
}
else if(head_count == 1)
{
setExtraTurn(player_id, false);
int points = getPoints(player_id);
if(points == winner_points)
setWinner(player_id, true);
}
else
{
int points = getPoints(player_id);
if(points > winner_points)
{
setExtraTurn(player_id, false);
setLoseTurn(player_id, true);
setPoints(player_id, points / 2);
}
else if(points == winner_points)
{
setExtraTurn(player_id, false);
    setWinner(player_id, true);
}
else
setExtraTurn(player_id, true);
}
}
void initPlayer(int player_id, const string& name)
{
setName(player_id, name);
setPoints(player_id, 0);
setWinner(player_id, false);
setLoseTurn(player_id, false);
setExtraTurn(player_id, false);
}
int selectFirstPlayer(void)
{
int player_id = rand() % 2;
return player_id;
}
int selectNextPlayer(int player_id)
{
int next_id = -1;
if(getWinner(player_id))
next_id = player_id;
else if(getExtraTurn(player_id))
next_id = player_id;
else if(getLoseTurn(player_id))
{
int other_id = otherId(player_id);
if(getLoseTurn(other_id))
{
setLoseTurn(other_id, false);
setLoseTurn(player_id, false);
}
next_id = other_id;
}
else
{
int other_id = otherId(player_id);
if(getLoseTurn(other_id))
{
setLoseTurn(other_id, false);
next_id = player_id;
}
else
next_id = other_id;
}
return next_id;
}
void computePoints(int player_id, int head_count)
{
int points = getPoints(player_id);
points = points + head_count;
setPoints(player_id, points);
}

void toss2Coins(int& coin_face1, int& coin_face2)
{
coin_face1 = rand() % 2;
coin_face2 = rand() % 2;
}
bool getWinner(int player_id)
{
if(player_id == 0)
return winner0;
else
return winner1;
}
void setWinner(int player_id, bool winner)
{
if(player_id == 0)
winner0 = winner;
else
winner1 = winner;
}
int getPoints(int player_id)
{
if(player_id == 0)
return points0;
else
return points1;
}
void setPoints(int player_id, int points)
{
if(player_id == 0)
points0 = points;
else
points1 = points;
}
string getName(int player_id)
{
if(player_id == 0)
return name0;
else
return name1;
}
void setName(int player_id, const string& name)
{
if(player_id == 0)
name0 = name;
else
name1 = name;
}
bool getLoseTurn(int player_id)
{
if(player_id == 0)
return lose_turn0;
else
return lose_turn1;

}
void setLoseTurn(int player_id, bool lose_turn)
{
if(player_id == 0)
lose_turn0 = lose_turn;
else
lose_turn1 = lose_turn;
}
bool getExtraTurn(int player_id)
{
if(player_id == 0)
return extra_turn0;
else
return extra_turn1;
}
void setExtraTurn(int player_id, bool extra_turn)
{
if(player_id == 0)
extra_turn0 = extra_turn;
else
extra_turn1 = extra_turn;
}
int otherId(int player_id)
{
int other_id = (player_id + 1) % 2;
return other_id;
}