質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.31%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

325閲覧

Gamecanvasを用いたオセロゲームが実行できない

kokokg

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2024/01/20 02:04

実現したいこと

gamecanvasを用いた、8*8マスの簡単なオセロゲームを作りたいのですが、エラーが出てしまいます。できたものは、学校での授業の期末課題として提出する予定です。
インターネットの情報などを参照しながら組み立てた初心者のコードです。的外れな質問をしていたらごめんなさい。

発生している問題・分からないこと

UNITYで実行しようとしたところ、以下のようなエラーメッセージが出ました。

エラーメッセージ

error

1All compiler errors have to be fixed before you can enter playmode! 2UnityEditor.SceneView:ShowCompileErrorNotification () (at /Users/bokken/build/output/unity/unity/Editor/Mono/SceneView/SceneView.cs:3919) 3 4Assets/Game.cs(31,23): error CS1061: 'IGameCanvas' does not contain a definition for 'width' and no accessible extension method 'width' accepting a first argument of type 'IGameCanvas' could be found (are you missing a using directive or an assembly reference?) 5 6Assets/Game.cs(53,16): error CS1061: 'IGameCanvas' does not contain a definition for 'GetPointerDown' and no accessible extension method 'GetPointerDown' accepting a first argument of type 'IGameCanvas' could be found (are you missing a using directive or an assembly reference?) 7 8Assets/Game.cs(55,46): error CS1061: 'IGameCanvas' does not contain a definition for 'pointerX' and no accessible extension method 'pointerX' accepting a first argument of type 'IGameCanvas' could be found (are you missing a using directive or an assembly reference?) 9 10Assets/Game.cs(56,46): error CS1061: 'IGameCanvas' does not contain a definition for 'pointerY' and no accessible extension method 'pointerY' accepting a first argument of type 'IGameCanvas' could be found (are you missing a using directive or an assembly reference?) 11 12Assets/Game.cs(78,81): error CS1503: Argument 5: cannot convert from 'UnityEngine.Color' to 'float' 13 14Assets/Game.cs(82,81): error CS1503: Argument 5: cannot convert from 'UnityEngine.Color' to 'float' 15 16Assets/Game.cs(86,20): error CS1061: 'IGameCanvas' does not contain a definition for 'StrokeRect' and no accessible extension method 'StrokeRect' accepting a first argument of type 'IGameCanvas' could be found (are you missing a using directive or an assembly reference?) 17 18

該当のソースコード

#nullable enable using GameCanvas; using Unity.Mathematics; using UnityEngine; using UnityEngine.InputSystem; /// <summary> /// ゲームクラス /// </summary> public sealed class Game : GameBase { // グリッドのサイズ private const int gridSize = 8; // セルのサイズ private float cellSize; // 盤面 private int[,] board = new int[gridSize, gridSize]; // 現在のプレイヤー(1: 黒, 2: 白) private int currentPlayer = 1; /// <summary> /// 初期化処理 /// </summary> public override void InitGame() { // キャンバスの大きさを設定します gc.ChangeCanvasSize(640, 640); // セルのサイズを計算 cellSize = gc.width / gridSize; // 盤面の初期化 for (int i = 0; i < gridSize; i++) { for (int j = 0; j < gridSize; j++) { board[i, j] = 0; } } // 初期配置 board[3, 3] = board[4, 4] = 1; board[3, 4] = board[4, 3] = 2; } /// <summary> /// 動きなどの更新処理 /// </summary> public override void UpdateGame() { // マウスがクリックされたら石を置く if (gc.GetPointerDown()) { int mouseX = Mathf.FloorToInt(gc.pointerX / cellSize); int mouseY = Mathf.FloorToInt(gc.pointerY / cellSize); if (IsValidMove(mouseX, mouseY)) { PlaceStone(mouseX, mouseY); SwitchPlayer(); } } } /// <summary> /// 描画の処理 /// </summary> public override void DrawGame() { // 盤面の描画 for (int i = 0; i < gridSize; i++) { for (int j = 0; j < gridSize; j++) { if (board[i, j] == 1) { gc.FillRect(i * cellSize, j * cellSize, cellSize, cellSize, Color.black); } else if (board[i, j] == 2) { gc.FillRect(i * cellSize, j * cellSize, cellSize, cellSize, Color.white); } // グリッド線の描画 gc.StrokeRect(i * cellSize, j * cellSize, cellSize, cellSize, Color.black); } } } // 石を置く処理 private void PlaceStone(int x, int y) { board[x, y] = currentPlayer; } // プレイヤーの切り替え private void SwitchPlayer() { currentPlayer = (currentPlayer == 1) ? 2 : 1; } // その位置に石を置けるかどうかの判定 private bool IsValidMove(int x, int y) { return (board[x, y] == 0); } }

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

chatgptに、エラーメッセージとコードを添えて問題点を聞いてみましたが、的確な回答を得られませんでした。

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

not be found と表記されているメソッドは存在しないメソッドを実行しようとしているようです。
下記の URL に存在するメソッドのみを実行する必要があるようです。

https://sfc-sdp.github.io/GameCanvas-Unity/api/GameCanvas.GcProxy.html#methods

例えば DrawRect というメソッドが存在するかどうか調べたい場合は
IGraphics の欄を見て、存在するか確認します。

また、FillRect で Color を設定したい場合は
事前に gc.SetColor(Color.black); 等を
事前に実行しておく必要があるようです。

GameBase に知識がある訳ではありませんので、
これでコンパイルエラーが消滅するか分かりませんが、
このコードでコンパイルエラーだけは消すことが出来ないでしょうか?

Unity(GameBase)

1#nullable enable 2using GameCanvas; 3using Unity.Mathematics; 4using UnityEngine; 5using UnityEngine.InputSystem; 6 7/// <summary> 8/// ゲームクラス 9/// </summary> 10public sealed class Game : GameBase 11{ 12 // グリッドのサイズ 13 private const int gridSize = 8; 14 15 // セルのサイズ 16 private float cellSize; 17 18 // 盤面 19 private int[,] board = new int[gridSize, gridSize]; 20 21 // 現在のプレイヤー(1: 黒, 2: 白) 22 private int currentPlayer = 1; 23 24 /// <summary> 25 /// 初期化処理 26 /// </summary> 27 public override void InitGame() 28 { 29 // キャンバスの大きさを設定します 30 gc.ChangeCanvasSize(640, 640); 31 32 // セルのサイズを計算 33 cellSize = gc.CanvasSize.x / gridSize; 34 35 // 盤面の初期化 36 for (int i = 0; i < gridSize; i++) 37 { 38 for (int j = 0; j < gridSize; j++) 39 { 40 board[i, j] = 0; 41 } 42 } 43 44 // 初期配置 45 board[3, 3] = board[4, 4] = 1; 46 board[3, 4] = board[4, 3] = 2; 47 } 48 49 /// <summary> 50 /// 動きなどの更新処理 51 /// </summary> 52 public override void UpdateGame() 53 { 54 // マウスがクリックされたら石を置く 55 if (gc.IsTouched()) 56 { 57 int mouseX = Mathf.FloorToInt(gc.LastPointerX() / cellSize); 58 int mouseY = Mathf.FloorToInt(gc.LastPointerY() / cellSize); 59 60 if (IsValidMove(mouseX, mouseY)) 61 { 62 PlaceStone(mouseX, mouseY); 63 SwitchPlayer(); 64 } 65 } 66 } 67 68 /// <summary> 69 /// 描画の処理 70 /// </summary> 71 public override void DrawGame() 72 { 73 // 盤面の描画 74 for (int i = 0; i < gridSize; i++) 75 { 76 for (int j = 0; j < gridSize; j++) 77 { 78 if (board[i, j] == 1) 79 { 80 gc.SetColor(Color.black); 81 gc.FillRect(i * cellSize, j * cellSize, cellSize, cellSize); 82 } 83 else if (board[i, j] == 2) 84 { 85 gc.SetColor(Color.white); 86 gc.FillRect(i * cellSize, j * cellSize, cellSize, cellSize); 87 } 88 89 // グリッド線の描画 90 gc.SetColor(Color.black); 91 gc.DrawRect(i * cellSize, j * cellSize, cellSize, cellSize); 92 } 93 } 94 } 95 96 // 石を置く処理 97 private void PlaceStone(int x, int y) 98 { 99 board[x, y] = currentPlayer; 100 } 101 102 // プレイヤーの切り替え 103 private void SwitchPlayer() 104 { 105 currentPlayer = (currentPlayer == 1) ? 2 : 1; 106 } 107 108 // その位置に石を置けるかどうかの判定 109 private bool IsValidMove(int x, int y) 110 { 111 return (board[x, y] == 0); 112 } 113}

投稿2024/01/22 01:57

uni2

総合スコア258

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.31%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問