所以我很难从一个类的整数返回值。
这是我的主程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MatchGame
{
class Program
{
static void Main(string[] args)
{
//( Amount of matches, bot argument, player argument, gameover?,)
//AI MatchGame = new AI(0, 0, 0, false, 0, false);
int matchAmount = 0;
int botMove = 0;
int playerMove = 0;
bool gameOver = false;
int playerMoveAttempt = 0;
bool choseRightAmount = false;
void TurnPlayer()
{
PlayersTurn PlayerMoved = new PlayersTurn(); //choseRightAmount, playerMoveAttempt, playerMove, matchAmount);
PlayersTurn.PlayerTurnMove(choseRightAmount, playerMoveAttempt, playerMove, matchAmount);
Console.WriteLine(playerMoveAttempt);
playerMove = playerMoveAttempt;
matchAmount = matchAmount - playerMove;
}
/*
void PlayerTurn()
{
choseRightAmount = false;
while (!choseRightAmount)
{
playerMoveAttempt = Convert.ToInt32(Console.ReadLine());
if (playerMoveAttempt < 1 || playerMoveAttempt > 3)
{
Console.WriteLine("Please take between 1 and 3 matches");
}
else
{
playerMove = playerMoveAttempt;
matchAmount = matchAmount - playerMove;
choseRightAmount = true;
}
}
} */
void BotTurn()
{
botMove = matchAmount % 4;
matchAmount = matchAmount - botMove;
}
void Status()
{
Console.WriteLine("Bot took " + botMove + " matches from the board.");
Console.WriteLine("There are " + matchAmount + " matches left.");
}
void Game()
{
Console.WriteLine("Welcome to the game!");
Console.WriteLine("To win you need to take the last match!");
Console.WriteLine("You can only take between 1 and 3 matches per turn.");
Console.WriteLine("Please choose the amount of matches you want.");
matchAmount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("You chose " + matchAmount + " It is your turn, please take between 1 and 3 matches");
while (!gameOver)
{
TurnPlayer();
if (matchAmount <= 0)
{
Console.WriteLine("You Won.");
break;
}
BotTurn();
if (matchAmount <= 0)
{
Console.WriteLine("You lost! The bot won the match!");
gameOver = true;
break;
}
Status();
}
Console.ReadKey();
}
Game();
}
}
}
所以我需要从该类中获取playerMove尝试的值。我知道我可以轻松地在主程序中执行此操作,但是我的老师希望我们使用课程。我需要在TurnPlayer()中使用它,以便我可以计算出剩余的匹配数。这在按钮的while循环中使用。
这是课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MatchGame
{
public class PlayersTurn
{
public static int PlayerTurnMove(bool choseRightAmount, int playerMoveAttempt, int playerMove, int matchAmount)
{
choseRightAmount = false;
while (!choseRightAmount)
{
playerMoveAttempt = Convert.ToInt32(Console.ReadLine());
if (playerMoveAttempt < 1 || playerMoveAttempt > 3)
{
Console.WriteLine("Please take between 1 and 3 matches");
}
else
{
choseRightAmount = true;
return playerMoveAttempt;
}
}
return playerMoveAttempt;
}
}
}
是的,正如您所看到的,我正在尝试退还playerMoveAttempt。我已经知道该类正在运行,只是不返回新值。
希望有些人可以提供帮助!预先感谢。
答案 0 :(得分:0)
您需要将方法调用的结果分配给变量,如下所示:
playerMoveAttempt = PlayersTurn.PlayerTurnMove(choseRightAmount, playerMoveAttempt, playerMove, matchAmount);
Console.WriteLine(playerMoveAttempt);
请记住,您可以在PlayerTurnMove
方法中执行的所有操作都不会更改其外部的playerMoveAttempt
,playerMove
和matchAmount
的值,尽管可以实现此目的行为。您应该阅读有关变量范围的信息。
由于您的方法只返回 playerMoveAttempt
的值,因此可以将您的方法的结果分配给变量,例如上面的代码示例。
我意识到这是一项家庭作业,您正在学习基础知识,只是想指出您应该考虑学习static
类/方法,何时使用它们以及何时不使用它们。尽管上面的代码示例应该可以工作,但是解决您的问题的更优雅的方法是使用您的类,如下所示:
public class PlayersTurn
{
public int PlayerMoveAttempt { get; set; }
public int PlayerMove{ get; set; }
public int MatchAmount{ get; set; }
public PlayersTurn(int playerMoveAttempt, int playerMove, int matchAmount)
{
this.PlayerMoveAttempt = playerMoveAttempt;
this.PlayerMove = playerMove;
this.MatchAmount = matchAmount;
}
public void PlayerTurnMove()
{
while (this.playerMoveAttempt < 1 || this.playerMoveAttempt > 3)
{
Console.WriteLine("Please take between 1 and 3 matches");
this.playerMoveAttempt = Convert.ToInt32(Console.ReadLine());
}
}
}
...然后在控制台应用程序中更改方法:
void TurnPlayer()
{
PlayersTurn playerMoved = new PlayersTurn(playerMoveAttempt, playerMove, matchAmount);
playerMoved.PlayerTurnMove();
Console.WriteLine(playerMoved.PlayerMoveAttempt);
playerMove = playerMoved.PlayerMoveAttempt;
matchAmount = playerMoved.MatchAmount - playerMoved.PlayerMove;
}
仍然不是最佳设计,因为在类中使用Console.ReadLine()
通常不是一个好主意,因为此方法与 Console Applications (控制台应用程序)高度结合,并且之所以起作用是因为在其中使用此类。
如果您想重用该类及其逻辑于另一种应用程序中(例如Windows窗体,Web应用程序),则此方法将无法正常工作。您的班级不应该知道用于从用户那里获取输入的机制。
我想现在就可以了,因为您只是在学习,也许您的老师的下一步就是完全重构它,但是如果您开始尝试理解这些概念,那就太好了。