所以我从一个类中返回一个整数的值时遇到了麻烦。
所以这是我的主要计划:
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()中使用它,这样我就可以计算剩余的匹配数量。这是在buttom中的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。我已经知道Class正在工作,它只是没有返回新的值。
希望你们有些人可以帮忙!提前致谢。
您需要将方法调用的结果分配给变量,如下所示:
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()
通常不是一个好主意,因为这种方法与控制台应用程序高度耦合,并且它的工作只是因为我们在其中使用这个类。
如果您曾想在其他类型的应用程序(例如Windows窗体,Web应用程序)中重用此类及其逻辑,则无法正常工作。您的类不应该知道用于从用户获取输入的机制。
我猜你现在好了,因为你刚刚学习,也许你老师的下一步就是重构这个,但如果你开始尝试理解这些概念,那就太好了。