teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

質問を追記しました。

2020/07/02 08:00

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- クラス間での配列、値の渡し方
1
+ ああああああああああああ
body CHANGED
@@ -1,127 +1,1 @@
1
- 現在C#を学んでおり、力試しでオセロを作っているのですが、クラス間での値の渡し方がわからず困っています。
2
- Form1クラスとGameクラスがあり、Gameクラスでは主に石を返したりなどのゲームの流れを処理するメソッドを管理しています。
3
- 質問として、現在Gameクラスのboard[,]配列とPlayer1変数,Player2変数がエラーになっており、(どちらも現在Form1に属しているため当たり前)これらをGameクラスでも使えるようにしたいです。
4
- 値を渡すといえばプロパティですが、調べた限りではプロパティは変数に値を代入するような形で使われており、今回のようなときに使えるのかがわかりませんでした。
5
- どうすれば値が渡せるのか、もしプロパティがつかえるのなら、どのように記述すればよいのか教えていただけると幸いです。
6
- 該当箇所にコメントで「エラー」と入れてあります。
7
- (時数オーバーのためGameクラスのみコードを載せます)
8
-
9
- Gameクラス
10
- ```
11
- using System;
12
- using System.Collections.Generic;
13
- using System.Linq;
14
- using System.Text;
15
- using System.Threading.Tasks;
16
-
17
- namespace WindowsFormsApplicationReversi
18
- {
19
- //ゲームの流れを管理するクラス
20
- class Game
21
- {
22
- //一方向のみ石を返す(動き)
23
- private static int Reverse(int y, int x, int m_y, int m_x)
24
- {
25
- int stone = CountStone(y, x, m_y, m_x);
26
- for (int i = 1; i <= stone; i++)
27
- {
28
- board[y + m_y * i, x + m_x * i] = Player1;//エラー
29
- }
30
- return stone;
31
- }
32
- //8方向に石を返す
33
- internal static int Reverse(int y, int x)
1
+ あああああああああああああああああああああああああああああああああああああああああ
34
- {
35
- int stone = 0;
36
- if (Check(y, x, 0))
37
- {
38
- stone += Reverse(y, x, 0, 1);//右
39
- stone += Reverse(y, x, 0, -1);//左
40
- stone += Reverse(y, x, 1, 0);//下
41
- stone += Reverse(y, x, -1, 0);//上
42
- stone += Reverse(y, x, 1, 1);//右斜め下
43
- stone += Reverse(y, x, -1, 1);//右斜め上
44
- stone += Reverse(y, x, 1, -1);//左斜め下
45
- stone += Reverse(y, x, -1, -1);//左斜め上
46
- if (stone > 0)
47
- {
48
- board[y, x] = Player1;//エラー
49
-
50
- stone++;
51
- Form1 form1 = new Form1();
52
- form1.CountStoneSub();
53
- }
54
- }
55
- return stone;
56
- }
57
- //一方向に返すことができる石の数
58
- private static int CountStone(int y, int x, int m_y, int m_x)
59
- {
60
- int x1 = x + m_x;
61
- int y1 = y + m_y;
62
- int stone = 0;
63
- while (Check(y1, x1, Player2))//エラー
64
- {
65
- x1 += m_x;
66
- y1 += m_y;
67
- stone++;
68
- }
69
- if (Check(y1, x1, Player1))//エラー
70
- {
71
- return stone;
72
- }
73
- else
74
- {
75
- return 0;
76
- }
77
- }
78
- //8方向に返すことができる石の数
79
- private static int CountStone(int y, int x)
80
- {
81
- int stone = 0;
82
- if (Check(y, x, 0))
83
- {
84
- stone += CountStone(y, x, 0, 1);//右
85
- stone += CountStone(y, x, 0, -1);//左
86
- stone += CountStone(y, x, 1, 0);//下
87
- stone += CountStone(y, x, -1, 0);//上
88
- stone += CountStone(y, x, 1, 1);//右斜め下
89
- stone += CountStone(y, x, -1, 1);//右斜め上
90
- stone += CountStone(y, x, 1, -1);//左斜め下
91
- stone += CountStone(y, x, -1, -1);//左斜め上
92
- }
93
- return stone;
94
- }
95
- //石を置けるかどうか
96
- internal static bool CanPut()
97
- {
98
- for (int y = 0; y <= 7; y++)
99
- {
100
- for (int x = 0; x <= 7; x++)
101
- {
102
- if (CountStone(y, x) > 0)
103
- {
104
- return true;
105
- }
106
- }
107
- }
108
- return false;
109
- }
110
- //指定マスに自分の石があるかどうか
111
- private static bool Check(int y, int x, int Player1)//エラー
112
- {
113
- return 0 <= x && x <= 7 && 0 <= y && y <= 7 && board[y, x] == Player1;//エラー
114
- }
115
- ////マスにすでに石があるかどうか
116
- //internal void StoneAlready(int y, int x)
117
- //{
118
- // if (board[y, x] != 0)
119
- // {
120
- // MessageBox.Show("そこにはおけません");
121
- // }
122
- //}
123
-
124
- }
125
- }
126
- ```
127
- ### 補足情報(FW/ツールのバージョンなど)