?? mower.cc
字號:
// mower.cc/* ---------------------------------------------------------------The Lawnmower ProblemAn example for how to use gpc++ - The Genetic Programming KernelThis program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 1, or (at your option)any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.Copyright 1996, 1997 Thomas WeinbrennerFor comments, improvements, additions (or even money !?) contact:Thomas WeinbrennerGrauensteinstr. 2635789 LaimbachGermanyE-mail: thomasw@emk.e-technik.th-darmstadt.deWWW: http://www.emk.e-technik.th-darmstadt/~thomasw--------------------------------------------------------------- */#include "gp.h"#include "lawn.h"// Reset lawn and mower. The lawn grows again, and the mower is// placed on the upper left corner facing south.void Mower::reset (){ for (int x=0; x<LawnHorizontal; x++) for (int y=0; y<LawnVertical; y++) lawn[x][y]=1; mown=0; direction=3; pos=Vector (0, 0);}// Print a lawnostream& operator << (ostream &os, Mower &mower){ for (int y=0; y<LawnVertical; y++) { for (int x=0; x<LawnHorizontal; x++) { // Mower position? if (mower.pos.x==x && mower.pos.y==y) os << 'M'; else os << (mower.lawn[x][y] ? 'G' : '.'); } os << '\n'; } return os;}// Move from the current position to one which is the addition of new// argvoid Mower::frog (Vector& v){ // New position (vector addition!) pos=pos+v; // Mow the grass in the area in which you have moved.... mowArea ();}// Turn the mower leftvoid Mower::left (){ direction = (direction + 1) % 4;}// Move the mower forward and mow that areavoid Mower::mow (){ switch (direction) { case 0: // East pos.x = (pos.x + 1 ) % LawnHorizontal; break; case 1: // North pos.y = (pos.y + (LawnVertical-1) ) % LawnVertical; break; case 2: // West pos.x = (pos.x + (LawnHorizontal-1) ) % LawnHorizontal; break; case 3: // South pos.y = (pos.y + 1 ) % LawnVertical; break; default: GPExitSystem ("Mower::mow", "Wrong switch statement"); } // mow the grass in the area in which you have moved.... mowArea ();}// Mow the area at current mower positionvoid Mower::mowArea (){ // Count how many areas are mown already mown+=lawn[pos.x][pos.y]; // Mow the grass in the area lawn[pos.x][pos.y]=0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -