הרשם שאלות ותשובות רשימת חברים לוח שנה הודעות מהיום

חזור   הוסטס - פורום אחסון האתרים הגדול בישראל > עיצוב גראפי, תכנות על כל שפותיו וקידום ושיווק אתרים > פורום תיכנות

   
|!|

השב
 
כלים לאשכול תצורת הצגה
ישן 17-05-15, 17:29   # 1
asaf1
חבר חדש
 
מיני פרופיל
תאריך הצטרפות: May 2015
הודעות: 2

asaf1 לא מחובר  

מבקש עזרה מכולם

עשיתי משחק איקס עיגול של שחקן נגד שחקן אך ביקשו ממני משחק של שחקן נגד מחשב.
המשחק בconsole.
זה הפתרון שלי:
static void SetBoard(char[,] board) //מאתחל לוח
{
string strBoard;
int intBoard;
for (int i = 0; i < board.GetLength(0); i++)
{
for (int z = 0; z < board.GetLength(1); z++)
{
intBoard = z + 1;
strBoard = Convert.ToString(intBoard);
board[z, i] = Convert.ToChar(strBoard);
//Console.WriteLine(board[z, i]); בדיקת איתחול
}
}
}
static void PrintBoard(char[,] board)//מדפיס לוח
{
for (int i = 0; i < board.GetLength(0); i++)
{
Console.Write("\n ");
for (int k = 0; k < board.GetLength(1); k++)
{
Console.Write("{0} ", board[k, i]);
}
}
Console.WriteLine();
}
static bool isWinner(char[,] board, char userType) //בודק אם יש מנצח
{
if (board[0, 0] == userType && board[1, 0] == userType && board[2, 0] == userType)
{
return true;
}
else if (board[0, 1] == userType && board[1, 1] == userType && board[2, 1] == userType)
{
return true;
}
else if (board[0, 2] == userType && board[1, 2] == userType && board[2, 2] == userType)
{
return true;
}
else if (board[0, 0] == userType && board[1, 1] == userType && board[2, 2] == userType)
{
return true;
}
else if (board[0, 2] == userType && board[1, 1] == userType && board[2, 0] == userType)
{
return true;
}
else if (board[0, 0] == userType && board[0, 1] == userType && board[0, 2] == userType)
{
return true;
}
else if (board[1, 0] == userType && board[1, 1] == userType && board[1, 2] == userType)
{
return true;
}
else if (board[2, 0] == userType && board[2, 1] == userType && board[2, 2] == userType)
{
return true;
}
else
{
return false;
}
}
static bool isTie(char[,] board) //בודק אם המשחק הגיע למצב של תיקו
{
foreach (char currentChar in board)
{
if (currentChar != 'X' && currentChar != 'O')
{
return false;
}
}
return true;
}
static void UpdateResult(char[,] board, bool isWinner, char userType, bool isTie, ref int scoreX, ref int scoreO, ref int gameNum, ref int ties) //מעדכן תוצאה
{
if (isWinner)
{
if (userType == 'X')
{
scoreX++;
}
if (userType == 'O')
{
scoreO++;
}
gameNum++;
}
if (isTie)
{
ties++;
gameNum++;
}
}
static void Main(string[] args)
{
bool validSelection = false;
char userType = '.';
bool userIsX = true;
int scoreX = 0;
int scoreO = 0;
int ties = 0;
int gameNum = 1;
char[,] board = new char[3, 3]; //הגדרת מערך לוח
while (true)
{
SetBoard(board); //קריאה לפונקציית אתחול לוח
if (gameNum == 1)
{
Console.BackgroundColor = ConsoleColor.Cyan;
Console.ForegroundColor = ConsoleColor.DarkCyan;
UpdateResult(board, isWinner(board, userType), userType, isTie(board), ref scoreX, ref scoreO, ref gameNum, ref ties); // מעדכן תוצאה
Console.WriteLine("\n GAME " + gameNum + ":\n Player X: " + scoreX + " | Player O: " + scoreO + " | Ties: " + ties + "\n"); //מעדכן תוצאה
Console.WriteLine(" BOARD");
PrintBoard(board); //קריאה לפונקציה מדפיסת לוח
Console.WriteLine("\n\n Welcome to Tic Tac Toe !\n\nThe rules are simple: Two players decide which is X and which is O, please note that X always goes first. In order to win, one must have three in a row, \nhorizontally, vertically or diagonally. If all nine spots are filled, then it \nis a tie, and you will be given the opportunity to play again. Let's begin!"); //הסברת המשחק
}
else
{
Console.WriteLine(" BOARD");
PrintBoard(board); //קריאה לפונקציה מדפיסת לוח
}
while (!isWinner(board, userType) && !isTie(board))
{
Console.WriteLine();
if (userIsX) //מגדיר שחקן X
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("Player: X");
userType = 'X';
}
else //מגדיר שחקן O
{
Console.BackgroundColor = ConsoleColor.DarkMagenta;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Player: O");
userType = 'O';

}
validSelection = false;
while (!validSelection) //כל עוד לא התבצעה בחירה חוקית
{
Console.WriteLine("Select row, then press 'enter' (return), then select column."); //הסבר על בחירת מיקום
int row = int.Parse(Console.ReadLine()); //משתמש בוחר מיקום
int column = int.Parse(Console.ReadLine());
if (row > 3 || row < 1 || column > 3 || column < 1) //בודק שהמספר בטווח
{
Console.WriteLine("Invalid row/column! There are only 3 rows and 3 columns on the board!\nPlease choose again.\n");
}
else if (board[column - 1, row - 1] == 'X' || board[column - 1, row - 1] == 'O') //בודק אם הבחירה חוקית
{
Console.WriteLine("You cannot overwrite you/your friend! Please choose again.");
}
else //מתקיים אם הבחירה תקינה
{
board[column - 1, row - 1] = userType;
validSelection = true;
}
}
userIsX = !userIsX;
Console.BackgroundColor = ConsoleColor.Magenta;
Console.ForegroundColor = ConsoleColor.Blue;
PrintBoard(board);
if (isWinner(board, userType)) //בודק אם הפונקציה שבודקת אם יש מנצח מחזירה true או false
{
Console.WriteLine("Congratulations! The winner is " + userType + "\n");
}
if (isTie(board)) //קריאה לפונקציה בודקת תיקו
{
Console.WriteLine("What a though game! The game resulted in a TIE!\n");
}
}
Console.BackgroundColor = ConsoleColor.DarkCyan;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Would you like to play again?\nPress 'enter' to play again, type 'n' then enter to quit."); //מאפשר יציאה מהתוכנית
string restart = Console.ReadLine();
if (restart == "n" || restart == "N")
{
break;
}
UpdateResult(board, isWinner(board, userType), userType, isTie(board), ref scoreX, ref scoreO, ref gameNum, ref ties); //מעדכן תוצאה
Console.WriteLine("\n GAME " + gameNum + ":\n Player X: " + scoreX + " | Player O: " + scoreO + " | Ties: " + ties + "\n"); //מדפיס תוצאה
}
}

אני מבקש בכל לשון של בקשה שמישהו ישנה את התוכנית שלי לכך שזה יהיה משחק מול מחשב.
תודה לעוזרים!
  Reply With Quote
ישן 18-05-15, 22:51   # 2
Tyler
חבר מתקדם
 
מיני פרופיל
תאריך הצטרפות: Jul 2013
הודעות: 417

Tyler לא מחובר  

קוד:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XmixDrix
{
classProgram
{
publicstructPlayer
{
publicstring Name;
publicXIgol mySign;
}
publicenumXIgol { Init = 0, X = 1, O = 2 }
staticvoid Main(string[] args)
{
Player p1, p2;
p1.Name = "Nir";
p1.mySign = XIgol.X;
p2.Name = "Ifat";
p2.mySign = XIgol.O;
int PlayerTurn=0;
int counter = 0;
int x,y;
XIgol type;
XIgol[,] board = newXIgol[3, 3];
initBoard(board);
printBoard(board);
while (counter < 9)
{
Console.WriteLine("Player {0}:",PlayerTurn+1);
do
{
Console.WriteLine("Insert x value y value and your type");
x = int.Parse(Console.ReadLine());
y = int.Parse(Console.ReadLine());
type = (XIgol)int.Parse(Console.ReadLine());
} while (checkIfCellIsOk(x,y,board) == false || (int)type != PlayerTurn+1);
assignValue(x, y, type, board);
printBoard(board);
if (checkWin(board, type) == true)
{
Console.WriteLine("Player {0} WIN!!!!:", PlayerTurn + 1);
counter = 20;
}
counter++;
PlayerTurn++;
PlayerTurn=PlayerTurn%2;
}
if(counter<20)
Console.WriteLine("No one win");
Console.ReadLine();
}
privatestaticvoid printBoard(XIgol[,] board)
{
for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
{
Console.Write(board[i,j] + "\t");
}
Console.WriteLine();
}
}
privatestaticvoid initBoard(XIgol[,] board)
{
for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
{
board[i, j] = XIgol.Init;
}
}
}
]
קוד:
        private bool CheckWin()
{
Color clr = Color.Blue;
if (turn)
{
clr = Color.Red;
}
int c1 = 0;
int c2 = 0;
int c3 = 0;
int c4 = 0;
int c5 = 0;
int c6 = 0;
int c7 = 0;
int c8 = 0;
for (int a = 0,x=2; a < 3;--x, ++a)
{
if (board[a, 0].BackColor == clr)
c1++;
if (board[a, 1].BackColor == clr)
c2++;
if (board[a, 2].BackColor == clr)
c3++;
if (board[0, a].BackColor == clr)
c4++;
if (board[1, a].BackColor == clr)
c5++;
if (board[2, a].BackColor == clr)
c6++;
if (board[a, a].BackColor == clr)
c7++;
if (board[a,x].BackColor == clr)
c8++;
}
if (c1 == 3) return true;
if (c2 == 3) return true;
if (c3 == 3) return true;
if (c4 == 3) return true;
if (c5 == 3) return true;
if (c6 == 3) return true;
if (c7 == 3) return true;
if (c8 == 3) return true;
return false;
}
__________________
facebook api, js, cpa, cpl, $$$
--------------------
שלח לי הודעה פרטית.
  Reply With Quote
ישן 23-05-15, 13:34   # 3
asaf1
חבר חדש
 
מיני פרופיל
תאריך הצטרפות: May 2015
הודעות: 2

asaf1 לא מחובר  

לא ככ הבנתי מה עשית פה: השתמשת בעצמים?
  Reply With Quote
השב

חברים פעילים הצופים באשכול זה: 1 (0 חברים ו- 1 אורחים)
 


חוקי פירסום
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is מופעל
סמיילים הם מופעל
[IMG] קוד מופעל
קוד HTML מכובה

קפיצה לפורום


כל הזמנים הם GMT +2. הזמן כעת הוא 04:48.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ