|
1023 | 1023 | " i+1"
|
1024 | 1024 | ]
|
1025 | 1025 | },
|
1026 | | - { |
1027 | | - "cell_type": "code", |
1028 | | - "execution_count": null, |
1029 | | - "metadata": {}, |
1030 | | - "outputs": [], |
1031 | | - "source": [ |
1032 | | - "# 2.36, skip 2.37\n", |
1033 | | - "import random\n", |
1034 | | - "#move adjacent cell\n", |
1035 | | - "# only fall in the same cell will decide being eaten or move back to original place and reproduce\n", |
1036 | | - "\n", |
1037 | | - "class Animal:\n", |
1038 | | - " def __init__(self, name, ori_location_id, ecolist):\n", |
1039 | | - " self._name = name\n", |
1040 | | - " self._ecolist = ecolist\n", |
1041 | | - " self._ori_location_id = ori_location_id\n", |
1042 | | - "\n", |
1043 | | - " def getname(self):\n", |
1044 | | - " return self._name\n", |
1045 | | - " \n", |
1046 | | - " def move(self, new_location):\n", |
1047 | | - " if not new_location is None:\n", |
1048 | | - " return False\n", |
1049 | | - " else: return True\n", |
1050 | | - " \n", |
1051 | | - " def is_same_species(self):\n", |
1052 | | - " if self._move():\n", |
1053 | | - "\n", |
1054 | | - "class Bear(Animal):\n", |
1055 | | - " def __init__(self, name, ori_location_id, ecolist):\n", |
1056 | | - " super().__init__(name, ori_location_id, ecolist)\n", |
1057 | | - " \n", |
1058 | | - " def eat(self, animaltype):\n", |
1059 | | - " pass\n", |
1060 | | - "\n", |
1061 | | - "class Fish(Animal):\n", |
1062 | | - " def __init__(self, name, ori_location_id, ecolist):\n", |
1063 | | - " super().__init__(name, ori_location_id, ecolist)\n" |
1064 | | - ] |
1065 | | - }, |
1066 | | - { |
1067 | | - "cell_type": "code", |
1068 | | - "execution_count": null, |
1069 | | - "metadata": {}, |
1070 | | - "outputs": [], |
1071 | | - "source": [ |
1072 | | - "class RiverEco:\n", |
1073 | | - "\n", |
1074 | | - " MOVE_CHANCE = 0.2\n", |
1075 | | - " LEFT_RIGHT = 0.5\n", |
1076 | | - "\n", |
1077 | | - " def __init__(self, totalnum, bearnum=2, fishnum=10):\n", |
1078 | | - " \n", |
1079 | | - " self._totalnum=totalnum\n", |
1080 | | - " self._bearnum=bearnum\n", |
1081 | | - " self._fishnum=fishnum\n", |
1082 | | - " self._ecolist = [None for i in range(totalnum)]\n", |
1083 | | - "\n", |
1084 | | - " def _init_assign_animal(self):\n", |
1085 | | - " randlist = random.sample(range(0,self._totalnum-1),self._bearnum+self._fishnum)\n", |
1086 | | - " bearlist = random.sample(randlist, self._bearnum)\n", |
1087 | | - " fishlist = randlist.copy()\n", |
1088 | | - " for i in randlist:\n", |
1089 | | - " if i in bearlist:\n", |
1090 | | - " fishlist.remove(i)\n", |
1091 | | - " \n", |
1092 | | - " for i in bearlist:\n", |
1093 | | - " self._ecolist[i]=Bear('B',i, self._ecolist)\n", |
1094 | | - " for i in fishlist:\n", |
1095 | | - " self._ecolist[i]=Fish('F',i, self._ecolist)\n", |
1096 | | - " \n", |
1097 | | - " def check_species(self, location1, location2):\n", |
1098 | | - " animal1, animal2 = self._ecolist[location1], self._ecolist[location2]\n", |
1099 | | - " if animal1.getname() == animal2.getname(): return True # return True = create new\n", |
1100 | | - " else: return False # return False = eat prey (eat Fish)\n", |
1101 | | - " \n", |
1102 | | - " def startgame(self):\n", |
1103 | | - " self._init_assign_animal()\n", |
1104 | | - " if random.random()>self.MOVE_CHANCE:\n", |
1105 | | - " for i in range(len(self._ecolist)):\n", |
1106 | | - " if self._ecolist[i] != None:\n", |
1107 | | - " if random.random()>self.LEFT_RIGHT and i+1<len(self._ecolist):\n", |
1108 | | - " if self._ecolist[i+1] != None:\n", |
1109 | | - " if self.check_species(i, i+1):\n", |
1110 | | - " self.createnew()\n", |
1111 | | - " else: self.remove(i,i+1)\n", |
1112 | | - " else:\n", |
1113 | | - " self._ecolist[i+1]=self._ecolist[i]\n", |
1114 | | - " self._ecolist[i] = None\n", |
1115 | | - " elif random.random()<=self.LEFT_RIGHT and i-1>=0:\n", |
1116 | | - " if self._ecolist[i-1] != None:\n", |
1117 | | - " if self.check_species(i, i-1):\n", |
1118 | | - " self.createnew()\n", |
1119 | | - " else: self.remove(i,i-1)\n", |
1120 | | - " else:\n", |
1121 | | - " self._ecolist[i-1]=self._ecolist[i]\n", |
1122 | | - " self._ecolist[i] = None\n", |
1123 | | - " self.checkeco()\n", |
1124 | | - " \n", |
1125 | | - " def checkeco(self):\n", |
1126 | | - " for i in range(1,len(self._ecolist)-1):\n", |
1127 | | - " if self._ecolist[i] != None:\n", |
1128 | | - " if self._ecolist[i-1] != None:\n", |
1129 | | - " if not self.check_species(i, i-1):\n", |
1130 | | - " self.remove(i,i-1)\n", |
1131 | | - " elif self._ecolist[i+1] != None:\n", |
1132 | | - " if not self.check_species(i, i+1):\n", |
1133 | | - " self.remove(i,i+1)\n", |
1134 | | - " \n", |
1135 | | - " def createnew(self):\n", |
1136 | | - " r = random.randint(0,len(self._ecolist)-1)\n", |
1137 | | - " while self._ecolist[r]==None:\n", |
1138 | | - " r = random.randint(0,len(self._ecolist)-1)\n", |
1139 | | - " name = self._ecolist[r].getname()\n", |
1140 | | - " if name == 'B':\n", |
1141 | | - " self._ecolist[i]=Bear('B', r, self._ecolist)\n", |
1142 | | - " elif name == 'F':\n", |
1143 | | - " self._ecolist[i]=Bear('F', r, self._ecolist)\n", |
1144 | | - " \n", |
1145 | | - " def remove(self, location1, location2):\n", |
1146 | | - " animal1, animal2 = self._ecolist[location1], self._ecolist[location2]\n", |
1147 | | - " if animal1.getname() == 'B':\n", |
1148 | | - " self._ecolist[location2] = None\n", |
1149 | | - " elif animal2.getname() == 'B': \n", |
1150 | | - " self._ecolist[location1] = None\n", |
1151 | | - " \n", |
1152 | | - " def printgame(self):\n", |
1153 | | - " eco=''\n", |
1154 | | - " for i in self._ecolist:\n", |
1155 | | - " if i != None:\n", |
1156 | | - " eco+=i.getname()\n", |
1157 | | - " else: eco+='-'\n", |
1158 | | - " print(eco)\n", |
1159 | | - " " |
1160 | | - ] |
1161 | | - }, |
1162 | | - { |
1163 | | - "cell_type": "code", |
1164 | | - "execution_count": null, |
1165 | | - "metadata": {}, |
1166 | | - "outputs": [], |
1167 | | - "source": [ |
1168 | | - "game1 = RiverEco(totalnum=100)\n", |
1169 | | - "i = 0\n", |
1170 | | - "for i in range(40):\n", |
1171 | | - " game1.startgame()\n", |
1172 | | - " game1.printgame()\n", |
1173 | | - " i+1" |
1174 | | - ] |
1175 | | - }, |
1176 | 1026 | {
|
1177 | 1027 | "cell_type": "code",
|
1178 | 1028 | "execution_count": null,
|
|
1310 | 1160 | ],
|
1311 | 1161 | "metadata": {
|
1312 | 1162 | "kernelspec": {
|
1313 | | - "display_name": "Python 3.7.13 ('deepcv')", |
| 1163 | + "display_name": "Python 3.9.12 ('deepcv')", |
1314 | 1164 | "language": "python",
|
1315 | 1165 | "name": "python3"
|
1316 | 1166 | },
|
|
1324 | 1174 | "name": "python",
|
1325 | 1175 | "nbconvert_exporter": "python",
|
1326 | 1176 | "pygments_lexer": "ipython3",
|
1327 | | - "version": "3.7.13" |
| 1177 | + "version": "3.9.12" |
1328 | 1178 | },
|
1329 | 1179 | "orig_nbformat": 4,
|
1330 | 1180 | "vscode": {
|
1331 | 1181 | "interpreter": {
|
1332 | | - "hash": "bc8728ca46fe0e46829d869d02c49a317711a0a5ed1f065b44257a00e89bb2e2" |
| 1182 | + "hash": "38e3adfb29cde9206c861f814c9fa42a1353f77e60ebe68d31b54d2673048b17" |
1333 | 1183 | }
|
1334 | 1184 | }
|
1335 | 1185 | },
|
|
0 commit comments