|
分類:[C#]
学校のクラブでカードゲームを作っているのですがなぜか配列の順番にカードが表示されません、どなたか助けてください・・・・
ちなみにゲームは次のカードが今のカードより低いか高いかを当てるやつです。
開発環境はC#Express
使用言語はC#です。
コードは以下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace TestCards
{
public partial class Form1 : Form
{
Cards myCards = new Cards();
Boolean myDraw = true;
//Int32 cardCount = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (myDraw)
{
Point myPoint = new Point(75, 50);
//if (cardCount >= 0)
//{
myCards.DrawCard(e, myCards.GetNewCard(), myPoint);
//myPoint.X += 105;
//}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void hButton_Click(object sender, EventArgs e)
{
myDraw = true;
this.Refresh();
int prevNum = myCards.previousNum();
int newNum = myCards.GetNewCard();
if (newNum > prevNum)
{
MessageBox.Show(this, "You Win!!");
}
else
{
MessageBox.Show(this,"You Lose!!");
}
}
private void lButton_Click(object sender, EventArgs e)
{
this.Refresh();
int prevNum = myCards.previousNum() % 12;
int newNum = myCards.GetNewCard() % 12;
//int v = x % 12;
//int z = y % 12;
if (newNum < prevNum)
{
MessageBox.Show(this, "You Win!!");
}
else
{
MessageBox.Show(this,"You Lose!!");
}
}
public class Cards
{
const int width = 79;
const int height = 123;
static Random _random = new Random();
int[] cardArray = new int [52];
int deckCardNumber;
int previousCardNumber;
public Cards()
{
deckCardNumber = 0;
for (int i = 0; i < 52; i++)
cardArray[i] = i;
Shuffle(cardArray);
}
public int GetNewCard()
{
deckCardNumber++;
deckCardNumber %= 52;
return cardArray.ElementAt(deckCardNumber);
}
public int previousNum()
{
deckCardNumber++;
deckCardNumber %= 52;
return cardArray.ElementAt(deckCardNumber-1);
}
//public int nextNum()
//{
// object n = cardArray[1];
// return cardArray[1];
//}
public void DrawCard(PaintEventArgs e, int cardNumber, Point p)
{
// Create a Bitmap object from a file.
Bitmap myBitmap = new Bitmap("..//..//cards.png");
int xOffset = 0; // TBD - Need to handle all cards
int yOffset = 0; // TBD - Need to handle all cards
if ((cardNumber <= 12) && (cardNumber >= 0))
{
xOffset = +cardNumber * width; // TBD - Need to handle all cards
}
else if ((cardNumber <= 25) && (cardNumber >= 13))
{
xOffset = +(cardNumber - 13) * width;
yOffset = +123;
}
else if ((cardNumber <= 38) && (cardNumber >= 26))
{
xOffset = +(cardNumber - 26) * width;
yOffset = +246;
}
else if ((cardNumber <= 51) && (cardNumber >= 39))
{
xOffset = +(cardNumber - 39) * width;
yOffset = +369;
}
else if ((cardNumber <= 53) && (cardNumber >= 52))
{
xOffset = +(cardNumber - 52) * width;
yOffset = +492;
}
else if (cardNumber >= 54)
{
xOffset = +158;
yOffset = +492;
}
// Clone a portion of the Bitmap object.
Rectangle cloneRect = new Rectangle(xOffset, yOffset, width, height);
System.Drawing.Imaging.PixelFormat format = myBitmap.PixelFormat;
Bitmap cloneBitmap = myBitmap.Clone(cloneRect, format);
// Draw the cloned portion of the Bitmap object.
e.Graphics.DrawImage(cloneBitmap, p);
}
public static void Shuffle<j>(j[]array)
{
var random = _random;
for (int i = array.Length; i > 1; i--)
{
// Pick random element to swap.
int k = random.Next(i);
// 0 <= j <= i-1
// Swap.
j tmp = array[k];
array[k] = array[i - 1];
array[i - 1] = tmp;
}
}
}
}
}
|