📄 population.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
namespace GA_RPS.GAKernel
{
class Population
{
private int popsize;
private int[] gaArray;
public int[] GaArray
{
get { return gaArray; }
set { gaArray = value; }
}
private int[] playerArray;
public int[] PlayerArray
{
get { return playerArray; }
}
private bool[] victory;
private int fitness = 0;
public int Fitness
{
get { return fitness; }
}
private int pointer = 0;
public Population()
{
this.popsize = GAControl.POP_SIZE;
gaArray = new int[popsize];
playerArray = new int[popsize];
victory = new bool[popsize];
}
public bool addMember(int ga, int player)
{
if (!isFull())
{
gaArray[pointer] = ga;
playerArray[pointer] = player;
victory[pointer] = isWin(this.gaArray[pointer], this.playerArray[pointer]);
if (victory[pointer]) fitness++;
pointer++;
return true;
}
else
{
return false;
}
}
public static bool isWin(int ga, int player)
{
switch (ga)
{
case GAControl.ROCK : switch( player)
{
case GAControl.ROCK : return false;
case GAControl.PAPER: return false;
default: return true;
}
case GAControl.PAPER: switch (player)
{
case GAControl.ROCK: return true;
case GAControl.PAPER: return false;
default: return false;
}
default : switch (player)
{
case GAControl.ROCK: return false;
case GAControl.PAPER: return true;
default: return false;
}
}
}
public bool isFull()
{
if (pointer == popsize)
return true;
else return false;
}
public String showHand(int hand)
{
if (hand == GAControl.ROCK)
return "rock";
else if (hand == GAControl.PAPER)
return "paper";
else
return "scissor";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -