如何在赛马比赛中移动马匹。 [英] How to move the horses in the horse race game.

查看:164
本文介绍了如何在赛马比赛中移动马匹。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

赛马游戏

在这个游戏中,屏幕上会出现一些马匹,从左边开始,比赛到右边的终点线。

每匹马的速度是随机确定的,所以没有办法提前弄清楚哪一个会赢。该程序使用控制台图形,所以马匹很容易,虽然有些粗糙,但是显示。



当我们的程序启动时,它会要求用户提供比赛的距离以及将在其中运行的马匹数量。赛马距离的经典单位是弗隆,距离是1/8英里。典型的比赛是6,8,10或12个弗隆。程序绘制与每个furlong相对应的垂直线,以及起始线和终点线。每匹马由一个矩形表示,中间有一个数字。

我尝试了不同的小步骤,但没有完全完成任务。

我在执行时移动了sigle horse。



我尝试过:



Horse Race Game
In this game a number of horses appear on the screen, and, starting from the left, race to a finish line on the right.
Each horse’s speed is determined randomly, so there is no way to figure out in advance which one will win. The program uses console graphics, so the horses are easily, although somewhat crudely, displayed.

When our program, is started, it asks the user to supply the race’s distance and the number of horses that will run in it. The classic unit of distance for horse racing is the furlong, which is 1/8 of a mile. Typical races are 6, 8, 10, or 12 furlongs. The program draws vertical lines corresponding to each furlong, along with start and finish lines. Each horse is represented by a rectangle with a number in the middle.
I tried different small steps which not accomplished the task completely.
I move the sigle horse at the execution time.

What I have tried:

#include <iostream>
#include <cstdlib>
#include<ctime>
#include <cstring>
#include <conio.h>
#include <windows.h>
#include <string>
using namespace std;
bool gameover;
int speed;
const int length = 20;
const int width = 20;
int x=0,y=0;
void draw()
{
	for (int i = 0; i < length; i++)
	{
		system("cls");
		for (int j = 0; j < width; j++)
		{
			if (j == x)
				cout << "[0]";
			else
			if (j % 2 == 0&&x<j)
				cout << "|";
			else
				cout << "   ";
		}
		x++;
		speed = rand() % 1000;
		Sleep(speed);
		cout << endl;
		{
			for (int j = 0; j < width; j++)
			{
				if (j == y)
					cout << "[1]";
				else
				if (j % 2 == 0 && y<j)
					cout << "|";
				else
					cout << "   ";
			}
			y++;
			speed = rand() % 1000;
			Sleep(speed);
			cout << endl;
		}
	}
}

int main()
{
	srand(time(NULL));	
	speed = rand() % 1000;
	{
		draw();
	}
	system("pause");
	return 0;
}

推荐答案

#include <iostream>
#include <ctime>
#include <Windows.h>
#include <stdlib.h>
using namespace std;

int FINISH;
void drawhorses(int * Horses,int m)
{
	int a=0;
	for (int i = 0; i <=m; i++)
	{
		if (i % 2 == 1)
		{
			cout << "[" << a << "]";
			++a;
			for (int k = Horses[i]; k < FINISH; k++)
			{
				cout << " | ";
			}
			for (int k = 0; k < Horses[i]; k++)
			{
				cout << " ";
			}
			cout << endl;
		}
		else
		{
			for (int k = 0; k <= FINISH; k++)
			{
				cout << " | ";
			}
			cout << endl;
		}
	}
}
void draw(int * Horses, int m)
{
	int b = 0;
	system("cls");
	for (int i = 0; i<=m; i++)
	{
		if (i % 2 == 1)
		{
			for (int k = Horses[i]; k>0; k--)
			{
				cout << "   ";
			}
			cout << "[" << b << "]";
			++b;
			for (int k = Horses[i]; k < FINISH; k++)
			{
				cout << " | ";
			}
			cout << endl;
		}
		else
		{
			for (int k = 0; k <= FINISH; k++)
			{
				cout << " | ";
			}
			cout << endl;
		}
	}
}

int main()
{
	int Nohorses,n=0;
	cout << "Enter the number of horses: ";
	cin >> Nohorses;
	system("cls");
	int dist;
	cout << "Enter the distance in forlong form 6 to 12: ";
	cin >> dist;
	FINISH = dist * 3;
	n = Nohorses * 2;
	system("cls");
	int *ptr;
	ptr = new int[n];
	srand(time(NULL));
	for (int i = 0; i<n; i++)
	{
		ptr[i] = 0;
	}
	drawhorses(ptr, n);
	int x = 0;
	bool end = false;
	while (!end)
	{
		Sleep(800);
		system("cls");
		for (int i = 0; i<n; i++)
		{
			x = i;
			ptr[i] += rand() % 2;
			if (ptr[i] >= FINISH)
			{
				end = true;				
			}
		}
		cout << "\n\n";
		draw(ptr, n);
	}
	delete ptr;
	system("cls");
	cout << "Horse number " << x/2 << " Won the race" << endl;
	system("pause");
	return 0;
}


你可以尝试随机改变马的速度,例如:



You could try to change randomly the horse speeds, for instance:

#include <iostream>
#include <stdlib.h>
using namespace std;

#define FURLONG_WIDTH 6
#define WIDTH   (10 * FURLONG_WIDTH)
#define PLAYERS 2

void draw(int x, int p)
{
  for (int n=0; n <= (WIDTH+1); ++n)
  {
    if (n == (x-1))
      cout << '[';
    else if (n == x)
      cout << p;

    else if (n==(x+1))
      cout << ']';
    else if ( n> 0 && (n % FURLONG_WIDTH) == 0)
      cout << '|';
    else
      cout << ' ';
  }
  cout << endl;
}

bool is_finished( bool finished[])
{
  for (int n=0; n<PLAYERS; ++n)
  {
    if (! finished[n] ) return false;
  }
  return true;
}

int main()
{ 
  double speed[PLAYERS];
  double pos[PLAYERS];
  bool finished[PLAYERS];
  
  for (int n=0; n<PLAYERS; ++n)
  {
    pos[n] = 1.0;
    speed[n] = 0.1 + (0.8 * rand()) / RAND_MAX;
    finished[n] = false;
  }



  while (! is_finished(finished))
  {
    for (int n=0; n<PLAYERS; ++n)
    {
      draw( static_cast <int> (pos[n]), n);

      if ( ! finished[n] )
      {
        if ( pos[n] >= WIDTH )
          finished[n] = true;
        pos[n] += speed[n];
        speed[n] +=  0.5 * rand() / RAND_MAX - .25;
        if ( speed[n] < 0.1)
          speed[n] = 0.0;
        else if ( speed[n] > 0.9)
          speed[n] = 0.9;
      }
      system("sleep 1");
    }
  }
}


这篇关于如何在赛马比赛中移动马匹。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆